添加“选择”下拉菜单中的项目
我正在使用 ASP.NET 动态数据。在 Insert.aspx 页面中,我有几个下拉列表可供选择。 Dropdown 表示的字段是数据库中的必填字段。因此,下拉列表不会将“选择”显示为下拉列表中的默认选项。我想在下拉列表中显示的数据库中的其他记录顶部添加“选择”选项。请注意,该字段不是必填字段,因此默认情况下动态数据不会显示“选择”选项。我怎样才能做到这一点?
I am using ASP.NET dynamic data. In Insert.aspx page, I have a couple of dropdowns to be selected. The fields represented by Dropdown are Required fields in database. So dropdown does not show 'Select' as default option in dropdown. I want to add 'Select' option at top of other records from database shown in dropdown. Note that field is not Required field, hence 'Select' option is not being show over there by dynamic data by default. How can I accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 Insert 方法绑定下拉列表后添加“选择”项:
Add your "select" item after binding your dropdownlists by using Insert method :
我将为必需的下拉字段创建一个自定义 FieldTemplate。在控件的 OnDataBinding 事件期间插入“Select”项。我还会有一个客户端RequiredFieldValidator,以确保在回发之前选择“Select”以外的其他内容。
-
-
您必须使用 UIHint 属性,以便使用此 FieldTemplate 代替默认值。
I would create a custom FieldTemplate for Required DropDown fields. Insert the "Select" item during the OnDataBinding event of the control. I would also have a client side RequiredFieldValidator to make sure something other than "Select" is chosen before it can post back.
-
-
you would have to use the UIHint attribute so that this FieldTemplate would be used over the default.
在索引 0 处添加顶部元素效果很好,但 LINQ 提供了另一种可能性,即返回“选择”或“选择某些内容”或“全部”作为数据的一部分。下拉列表已数据绑定到此函数:
Adding a top element at index 0 works just fine, but LINQ offers another possibility, to return the "select" or "pick something" or "all" as part of the data. A drop-down list was databound to this function:
从哑表中选择前 1 个“0”作为 valueField,“--Select--”作为 textField
联盟
从后端的表格控件中选择 ID、文本
提供了更大的灵活性
Select Top 1 '0' as valueField, '--Select--' as textField from dummtable
UNION
select ID,text from yourTable
Control from backend gives much more flexibility