C# Gridview 模板行中的条件评估
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18859152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Conditional Eval in Gridview Template row
提问by Paul T. Rykiel
I am having an issue that I am certain is easy to resolve, I just don't know what to do. Here is my code:
我遇到了一个我确信很容易解决的问题,我只是不知道该怎么办。这是我的代码:
<asp:TemplateField>
<HeaderTemplate>
<asp:Literal ID="text_shipped" Text="Media Shipped" runat="server" />
<br />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbl_shipped" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "shipped") %>' />--></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<asp:Button ID="lnk_ship" runat="server" CssClass="btn-mini" Text="Ship Software" Visible='<%# DataBinder.Eval(Container.DataItem, "shipped" ) == "Yes" ? true : false %>' />--></ItemTemplate>
</asp:TemplateField>
The label "lbl_shipped" is showing the correct value which is either "Yes" or "No" but, i want to add a button "lnk_ship", based on whether or not the value is "Yes" (show button), or "No" (do not show button).
标签“lbl_shipped”显示了正确的值,即“是”或“否”,但是,我想根据值是否为“是”(显示按钮)或“否”(不显示按钮)。
My issue is that I am using conditional code on the Visible keyword and I am testing for the value, but it seem to be ignoring my value for "shipped"
我的问题是我在 Visible 关键字上使用条件代码并且我正在测试该值,但它似乎忽略了我的“已发货”值
here are the main two lines, the first one shows the value, the second line is conditional, the conditional is NOT working. it keeps showing false:
这是主要的两行,第一行显示值,第二行是有条件的,条件不起作用。它一直显示错误:
<asp:Label ID="lbl_shipped" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "shipped") %>' />
<asp:Button ID="lnk_ship" runat="server" CssClass="btn-mini" Text="Ship Software" Visible='<%# DataBinder.Eval(Container.DataItem, "shipped" ) == "Yes" ? true : false %>' />
采纳答案by Scotty.NET
I have just mocked up something quickly and it is working for me (ASP.NET web forms targeting .NET 4, VS2012) , maybe have a look:
我刚刚快速模拟了一些东西,它对我有用(针对 .NET 4、VS2012 的 ASP.NET Web 表单),也许看看:
Default.aspx
默认.aspx
Contains the following GridView
definition which I stuck randomly in a new web forms project.
包含以下GridView
定义,我在一个新的 Web 表单项目中随机粘贴了这些定义。
<asp:GridView runat="server" ID="gridMe" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Button runat="server" ID="btnName" Text="Hi" Visible='<%# DataBinder.Eval(Container.DataItem, "Name").ToString() == "Bob" %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Default.aspx.cs
默认.aspx.cs
Has the following class definitions
具有以下类定义
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
var list = new List<Thing>
{
new Thing() {ID = 1, Name = "Bob"},
new Thing() {ID = 2, Name = "Geraldine"}
};
gridMe.DataSource = list;
gridMe.DataBind();
}
}
public class Thing
{
public int ID { get; set; }
public string Name { get; set; }
}
Result
结果
My output is something like:
我的输出是这样的:
ID Name
1 [Hi]
2
回答by Irfan TahirKheli
DataBinder.Eval(Container.DataItem, "shipped" ).ToString()
Add .ToString() ie:
添加 .ToString() 即:
<asp:Button ID="lnk_ship" runat="server" CssClass="btn-mini" Text="Ship Software" Visible='<%# DataBinder.Eval(Container.DataItem, "shipped" ).ToString() == "Yes" ? true : false %>' />