ASP.NET GridView ItemTemplate [英] ASP.NET GridView ItemTemplate
问题描述
好的,我有一个GridView,并且如果文件存在,我想要成为一个链接,否则我只是希望它成为一个标签。现在,我正在使用参数中传递的行更改RowDataBound事件处理函数上的控件。我不是很喜欢这个,因为我很难编码列ID,如果它改变了,我将需要记住改变这个代码。我希望我可以在asp代码中做一个条件来添加一个链接,如果一个属性值不是null,否则添加一个标签。这可能吗?任何不同的解决方案?
OK I have a GridView and there is a column that I want to be a link if a file exists, otherwise I just want it to be a label. Right now I am changing the controls on RowDataBound event handler using the Row passed in the args. I am not a big fan of this as I am hard coding the column ID, and if it ever changes I will need to remember to change this code. I was hoping I could do a conditional in the asp code to add a link if a property value is not null otherwise add a label. Is this possible? Any different solutions?
我想这样的事情:
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<%# if (Eval("LogFileName") == null)
<%#{
<asp:LinkButton ID="LogFileLink" runat="server" CommandArgument='<% #Eval("LogFileName") %>' CommandName="DownloadLogFile" Text='<%# Blah.NDQA.Core.Utilities.GetEnumerationDescription(typeof(Blah.NDQA.Core.BatchStatus), Eval("Status")) %>'>
<%# }
<%# else
<%#{
<asp:Label ID="LogFileLabel" runat="server"Text='<%# Blah.NDQA.Core.Utilities.GetEnumerationDescription(typeof(Blah.NDQA.Core.BatchStatus), Eval("Status")) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
推荐答案
如果您要这么做,我建议写你自己的领域。最简单的方法可能是使一个NullableHyperlinkField继承自HyperlinkField,并且如果该锚点的URL否则为空,则渲染出一个普通字符串。
If you're going to be doing this a lot, I suggest writing your own field. The simplest approach is probably to make a NullableHyperlinkField inheriting from HyperlinkField, and render out a plain string if the anchor's URL would otherwise be null.
这篇关于ASP.NET GridView ItemTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!