开发者

How to display a resource value in a gridview column, depending on another column's value?

Here's what I have (working):

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>                        
    <asp:BoundField HeaderText="Id" DataField="Id" SortExpression="Id">
    </asp:BoundField>
    <asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name">
    </asp:BoundField>
    <asp:TemplateField>                        
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<% $Resources: Resource, strYes %>' />
        <br />
        <asp:Label ID="Label2" runat="server" Text='<%# Equals("name1",Eval("Name")) %>' />
        <br />
        <asp:Label ID="Label3" runat="server" Text='<%# Eval("Name") %>' />
        <br />
    </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>

What I wanted is something like (not working):

<asp:Label ID="Label4" runat="server" Text='<% (Equals("a",Eval("Name"))) $Resources: Resource, strYes : $Resources: Resource, strNo  %>' /&开发者_如何学JAVAgt;

but which I haven't found out how to make it work! (So, depending on a previous column's value, show one or the other Resource item).

Any suggestions to correct this? Thanks!


I think the easiest way is to use the return value of a function as text for your label:

<asp:Label Text="<%# GetLabelText(Container.DataItem) %>" ID="Label1" runat="server" />

And then in the .cs file

protected string GetLabelText(object dataItem) {
    DataRowView dataRowView = (DataRowView)dataItem;
    string name = (string)dataRowView.Row["Name"];
    return "some string with some logic";
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜