If the operator is in the repeater

I have this repeater on my page. In the default column, I want it to be an IF condition that checks the value of the table field "IsDEfault". If IsDefault = True, then under the table below "label1", that is, "Yes" should be displayed inside the repeater, the link "Make DEfault" should be displayed.

Now, how do I include this IF statement as inline code in the repeater to accomplish what I want?

<asp:LinkButton ID="lnk1" Text="Make Default" CommandName="SetDefault" runat="server" Visible="True" CommandArgument='<%#Eval("UserID") %>' CausesValidation="false"></asp:LinkButton> <asp:Label ID="label1" Text="Yes" runat="server" Visible="False"></asp:Label> 

I have an idea: -

 <%# If DataBinder.Eval(Container.DataItem,"IsDefault") = "True" Then%> <%End If%> 

How do I now form the expression "Then"? Please help me with the correct syntax ..thnx Do I need to make a method that checks if IsDefault is true or not, and then calls it inside the inline code in my repeater? How can I do it?

[EDIT]

I tried to do the following: -

 <% If (Eval("Container.DataItem,"IsDefault"")="True"? ("<asp:LinkButton ID="lnk1" Text="Set as Default" CommandName="SetDefault1" runat="server" CommandArgument='<%#Eval("User1ID") %>' CausesValidation="false" Visible=true></asp:LinkButton>") : ("<asp:Label ID="label1" Text="Yes" runat="server" Visible=true></asp:Label>") )%> 

Doesn't work :( Help !!

+7
source share
3 answers

Here is your repeater markup. Note that both controls are hidden at the beginning:

 <asp:Repeater runat="server" ID="rpt1" OnItemDataBound="rpt1_ItemDataBound" onitemcommand="rpt1_ItemCommand"> <ItemTemplate> <p> ID: <%# Eval("Id") %> IsDefault: <%# Eval("IsDefault") %> Name: <%# Eval("Name") %> <asp:Label BackColor="Blue" ForeColor="White" runat="server" ID="lDefault" Text="DEFAULT" Visible="false" /> <asp:Button runat="server" ID="btnMakeDefault" Text="Make Default" Visible="false" CommandArgument='<%# Eval("Id") %>' /> </p> </ItemTemplate> </asp:Repeater> 

And some code to go with it. Note. I modeled the extraction of your collection of blluser objects, so there is some additional code related to this that you will not need, since apparently the collection of bllusers that you are contacting comes from db or something else?

In any case, I think this is what you are looking for, but let me know if it is not; -)

  //Dummy object for illustrative purposes only. [Serializable] public class bllUsers { public int Id { get; set; } public bool isDefault { get; set; } public string Name { get; set; } public bllUsers(int _id, bool _isDefault, string _name) { this.Id = _id; this.isDefault = _isDefault; this.Name = _name; } } protected List<bllUsers> lstUsers{ get { if (ViewState["lstUsers"] == null){ ViewState["lstUsers"] = buildUserList(); } return (List<bllUsers>)ViewState["lstUsers"]; } set{ ViewState["lstUsers"] = value; } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { buildGui(); } } private List<bllUsers> buildUserList(){ lstUsers = new List<bllUsers>(); lstUsers.Add(new bllUsers(1, false, "Joe Bloggs")); lstUsers.Add(new bllUsers(2, true, "Charlie Brown")); lstUsers.Add(new bllUsers(3, true, "Barack Obama")); return lstUsers; } private void buildGui() { rpt1.DataSource = lstUsers; rpt1.DataBind(); } protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { bllUsers obj = (bllUsers)e.Item.DataItem;//this is the actual bllUser the row is being bound to. //Set the labels ((Label)e.Item.FindControl("ldefault")).Visible = obj.isDefault; ((Button)e.Item.FindControl("btnMakeDefault")).Visible = ! obj.isDefault; //Or use a more readable if/else if you want: if (obj.isDefault) { //show/hide } else { //set visible/invisible } } } 

Hope this helps :-)

+3
source share

If you want a control to be visible only under certain conditions, set the Visible property according to this condition:

 <asp:Label ID="label1" Text="Yes" runat="server" Visible="<%# DataBinder.Eval(Container.DataItem,"IsDefault") %>" /> 

EDIT
If you want to control INvisible for the β€œIsDefault” situation, change the test to something like Visible="<%# DataBinder.Eval(Container.DataItem,"IsDefault")==False %>" . I'm not entirely sure about the exact syntax, but you should get this idea.

+4
source share

Sorry that you are honest, I could not understand what you really wanted to do. If you want to use the condition in Item Templet, then I think the following systax will help you

 <asp:LinkButton ID="Label1" runat="server" Text='<%# ((Eval("Cond"))="True" ? Eval("Result for True") : Eval("Result for False") )%>'></asp:LinkButton> 
+1
source share

All Articles