开发者

If statement in repeater

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 synta开发者_如何学JAVAx..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!!


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.


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 :-)


Sorry to say you to be honest i was unable to understand what actually you wanted to do If your looking to use the condition in the 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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜