如何以编程方式确定在 EditItemTemplate 中使用哪个控件? (ASP.NET)
在我的 ASP.NET 应用程序中,我有一个 GridView。对于此 GridView 中的特定字段,我添加了带有 DropDownList 的 EditItemTemplate。但是,如果该字段的值为“X”,那么我只想显示一个标签而不是 DropDownList。那么我如何以编程方式检查字段值,然后决定显示哪个控件?
这是我的 EditItemTemplate:
<EditItemTemplate>
<asp:DropDownList ID="DropDownListLevel_ID" runat="server"
DataSourceID="ODSTechLvl" DataTextField="Level_Name"
DataValueField="Level_ID" SelectedValue='<%# Bind("Level_ID", "{0}") %>'>
</asp:DropDownList>
</EditItemTemplate>
如果 Level_ID 的值为“X”,那么我想使用:
<asp:Label ID="LabelLevel_ID" runat="server" Text='<%# Bind("Level_ID") %>'></asp:Label>
而不是 DropDownList。
我尝试在 DropDownList 之前嵌入 if 语句来检查 Eval("Level_ID"),但这似乎不起作用。有什么想法吗?
In my ASP.NET application, I have a GridView. For a particular field in this GridView, I've added an EditItemTemplate with a DropDownList. However, if the value of the field is "X", then I want to just display a label instead of the DropDownList. So how can I programatically check the field value, then decide which control to display?
Here is my EditItemTemplate:
<EditItemTemplate>
<asp:DropDownList ID="DropDownListLevel_ID" runat="server"
DataSourceID="ODSTechLvl" DataTextField="Level_Name"
DataValueField="Level_ID" SelectedValue='<%# Bind("Level_ID", "{0}") %>'>
</asp:DropDownList>
</EditItemTemplate>
If the value of Level_ID is "X", then I want to use:
<asp:Label ID="LabelLevel_ID" runat="server" Text='<%# Bind("Level_ID") %>'></asp:Label>
instead of the DropDownList.
I tried embedding an if statement before the DropDownList to check Eval("Level_ID"), but that doesn't seem to work. Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
Try this:
这是适用于 ASP.Net 的东西。
您可以创建 RowDataBound 事件并隐藏标签或 DropDownList
... >
...
并且在后面的代码中:
*注意这不太理想,因为它对单元格索引进行硬编码,并且控件的 ID 是一个字符串,直到运行时才会检查。在 ASP.NET MVC 中有很多更优雅的方法来解决这个问题。
OnRowDataBound 是一把大锤,它可以让您完全访问网格、页面方法和整个应用程序。在非常简单的场景中,您也可以内联执行此操作,而无需涉及代码隐藏。
或者
在第一个内联方法中,您的数据源必须公开这样的属性,而在第二种方法中,您将specialValue业务逻辑硬编码到您的演示文稿中,这也很丑陋,并且会导致可维护性问题。
Here is something that will work for ASP.Net.
You could create an RowDataBound event and hide the label or the DropDownList
... >
...
and in your code behind:
*note this is less than ideal because it hard codes the cell index, and the ID of the controls is a string that wont be checked until runtime. There are much more elegant ways to solve this problem in asp.net mvc.
the OnRowDataBound is a sledge hammer that will give you full access to your grid, page methods, and your entire application. In a very simple scenario you could also do it inline without involving the codebehind.
or
in the first inline approach, your data source has to expose such a property, and in the second you are hard coding your specialValue business logic into your presentation, which is also ugly and will lead to problems with maintainability.