带下拉列表的 Asp.net 动态网格视图
我有一个动态(允许动态添加行)的 ASP 网格视图,其中一列中有一个下拉列表。我想在数据输入期间根据下拉列表中的选择采取操作启用/禁用列中的文本框。
任何帮助将不胜感激。
I have a dynamic (allows addition of rows on the fly) an ASP gridview that has a dropdownlist in one of its columns. I would like to take an action enable/disable the textbox in the column after depending on the selection in the dropdownlist during data entry.
Any help will be highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 jQuery 轻松完成此操作。通过一些修改,您就可以让它完全按照您想要的方式工作。
首先,将以下内容添加到您的
标记中:
接下来,创建网格视图并为文本框和下拉列表添加模板列。在下面的代码中,请注意下拉列表已被指定为“ddlClass”类,并且文本框已被指定为“txtClass”类。您可以根据需要更改这些内容。
.ready 函数将单击事件附加到类为“ddlClass”的每个下拉列表。更改后,代码将在与下拉列表相同的行中找到类为“txtClass”的文本框,然后相应地启用/禁用。
You can do this easily with jQuery. With a bit of modification, you can get it working exactly as you want it to.
First, add the following to your
<head>
tag:Next, create your gridview and add template columns for your textbox and dropdown list. In the code below, notice that the dropdown list has been given a class "ddlClass" and the textbox has been given a class "txtClass". You can change these as you see fit.
The .ready function attaches a click event to each dropdownlist with the class "ddlClass". When changed, the code will find the textbox with the class "txtClass" in the same row as the dropdownlist and then enable/disable accordingly.
如果您熟悉 Javascript,那么您可以使用它。我建议使用 JQuery,因为它是一种用于遍历 DOM 的查询语言。
但如果您不熟悉 Javascript,那么我建议您在 DropDownList 上添加 SelectionChangedEvent,然后在 SelectionChangedEvent 处理程序中的页面后面的代码中添加:
将发送者对象转换为 DropDownList,然后获取该对象的父对象(即 GridViewRow)。
通过该 GridViewRow,您可以使用 FindControl 方法来获取对同一行中的 TextBox 的引用,然后您可以启用或禁用它。
如果您不喜欢每次更改下拉列表中的选择时页面刷新(回发),请将网格包装在 UpdatePanel 中。
如果您对此遇到困难,请告诉我,我会将代码发布到上述解决方案之一。请告诉我您对哪一款最满意。
Well you can use Javascript if you are familiar with that. I recommend using JQuery since its a query language for transversing through the DOM.
But if you are not familiar with Javascript then I recommend adding a SelectionChangedEvent on your DropDownList and then in the code behind for your page in the SelectionChangedEvent handler:
Cast the sender object to a DropDownList and then get the parent of that object which would be the GridViewRow.
With that GridViewRow you can use the FindControl method to get a reference to the TextBox in the same row and then you can enable it or disable it.
If you dont like the page refresh (post-back) everytime they change a selection in your dropdownlist then wrap your grid in an UpdatePanel.
Let me know if you are having a hard time with this and I'll post code to 1 of the above solutions. Just let me know which one you are most comfortable with.