开发者

DataRepeater displays different columns in each entry

I have a Repeater. It gets data from some table . This table has say 3 columns.

What I want is if column1 = true column2 will be displayed and if c开发者_StackOverflow社区olumn1 = false column 3 will be displayed.

The layout will be something like this:

c1 = true  | c2
c1 = true  | c2
c1 = false | c3
c1 = true  | c2
c1 = false | c3

How can I achieve that?


Just using a Literal in your Repeater template:

<asp:Repeater runat="server" ID="rep"> 
    <ItemTemplate> 
        <asp:Literal runat="server" ID="lit1" OnDataBinding="lit1_DataBinding"></asp:Literal> 
    </ItemTemplate> 
</asp:Repeater>

Then implement the DataBinding:

protected void lit1_DataBinding(object sender, System.EventArgs e)
{
    Literal lit = (Literal)(sender);
    lit.Text = ((bool)(Eval("Column1")) ?
        Eval("Column2").ToString() : Eval("Column3").ToString();
}


You can do everything declaratively without code-behind:

<asp:Repeater runat="server" ID="rpt">
    <ItemTemplate>
        <%# (bool)Eval("Column1") ? Eval("Column2") : Eval("Column3") %><br />
    </ItemTemplate>
</asp:Repeater>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜