Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If statement in repeater

Tags:

c#

asp.net

I have this repeater on my page..Under the default column what I want is that there should be an IF condition that checks my table's "IsDEfault" field value. If IsDefault=True then the lable below "label1" ie "Yes" should be displayed inside the repeater else "Make DEfault" link should be displayed..

Now how do I include this IF statement as inline code in my 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 should I form the "Then" statement now? Please help me with the proper syntax..thnx Do I need to like make a method that checks if "IsDefault" is true or not and then call it inside of inline code in my repeater ? How do I go about it ?

[EDIT]

I tried as follows:-

<% 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>")
)%>

didnt work :( Help!!

like image 945
Serenity Avatar asked Nov 19 '10 05:11

Serenity


2 Answers

If you want some control to be visible only on some condition, set the Visible property according to that condition:

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

EDIT
If you want the control INvisible for the "IsDefault" situation, reverse the test with something like Visible="<%# DataBinder.Eval(Container.DataItem,"IsDefault")==False %>". I'm not quite sure about the exact syntax, but you should get the idea.

like image 53
Hans Kesting Avatar answered Oct 12 '22 04:10

Hans Kesting


Here's your repeater markup. Notice both controls are hidden at the start:

<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've simulated the retrieval of your collection of blluser objects, so there is some additional code there relating to this which you won't require as, presumably the bllusers collection that you bind to is coming from a db or something?

Anyway I think this is what you're looking for, but let me know if its 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 :-)

like image 26
indra Avatar answered Oct 12 '22 04:10

indra