如何绑定到没有值的下拉列表 [英] How to bind to a dropdown list with no value

查看:159
本文介绍了如何绑定到没有值的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一些代码来进行gridview的批量更新。在gridview中,我的一个模板字段有一个下拉列表:

 < asp:DropDownList ID =DDLCategoriesrunat =服务器
DataSourceID =odsCategoriesDataTextField =txtCategory
DataValueField =intCategoryIDSelectedValue ='<%#Bind(intCategoryID)%>'>
< asp:ListItem Text =请选择一个项目Value = - 1>< / asp:ListItem>
< / asp:DropDownList>

在我的数据库中我有800条记录。但是,只有其中的几个被赋值为intCategoryID。其余的都是NULL。



批量更新的全部原因是更新所有的intCategoryID字段,并为每个记录分配一个类别ID。

因为不是所有的odintCategoryID都被赋值了,当我尝试渲染页面时,我收到一个错误。下拉列表失败。这是消息:

 'DDLCategories'有一个SelectedValue,因为它不存在于项目列表中,因此无效。 
参数名称:值

有没有办法可以将空类别分配给列表我在下拉列表中创建了值为-1的项目? IE如果intCategoryIDISNULL然后选择了请选择一个项目,否则选择所选值。

解决方案

查看这篇文章。它覆盖DDL的绑定方法,并检查缺少的SelectedValue。如果您不使用FormView,您可能需要修改它,但我认为您可以从该帖子开始。


I have written some code to do a bulk update of a gridview. In the gridview one of my template fields has a drop downlist:

<asp:DropDownList ID="DDLCategories" runat="server" 
  DataSourceID="odsCategories" DataTextField="txtCategory" 
  DataValueField="intCategoryID" SelectedValue='<%# Bind("intCategoryID") %>'>
  <asp:ListItem Text="Please Select an item" Value ="-1"></asp:ListItem>
</asp:DropDownList>

In my database I have 800 records. However only a few of them have been assigned the value of "intCategoryID". The rest are NULL.

The whole reason of the bulk update is to update all of the "intCategoryID" fields and assign a category ID to each record.

Because not all od the "intCategoryID" have been assigned a value I am getting an error when I try to render the page. The dropdown list is failing. Here is the message:

'DDLCategories' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value 

Is there a way I can assign the empty categories to the list item that has a value of "-1" I have created in the dropdownlist? IE If the "intCategoryID" ISNULL then have "Please Select an item" selected, otherwise select the selected value.

解决方案

Check out this post. It overrides the binding method for the DDL and checks for the missing SelectedValue. You may have to modify it if you're not using a FormView, but I think you'll get a good start with that post.

这篇关于如何绑定到没有值的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆