我正在使用V2005 C#。
我的.aspx页面中有一个.aspx,我能够使用.aspx中的DropDownList控件更新数据库。
DDL用于我的性别专栏:
<asp:TemplateField HeaderText="Gender" SortExpression="Gender">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" SelectedValue='<%# Bind("Gender") %>'>
<asp:ListItem>M</asp:ListItem>
<asp:ListItem>F</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Gender") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>在我的性别列的DDL EditItemTemplate中,我使用了硬编码的值,它可以工作。
但是,我尝试在另一列上实现DDL。这一次,我使用了一个SqlDataSource,它从另一个表中选择数据,而不是使用固定值,但是它们给了我一个错误:'DropDownList3' has a SelectedValue which is invalid because it does not exist in the list of items.Parameter name: value。
我也试图实现SelectedValue='<%# Bind("MemberType") %>',但它没有工作。
下面是我的MemberType DDL EditItemTemplate的代码:
<asp:TemplateField HeaderText="MemberType" SortExpression="MemberType">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource2"
DataTextField="MemberType" DataValueField="MemberType" SelectedValue='<%# Bind("MemberType") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("MemberType") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>有人知道这是怎么回事吗?
谢谢
发布于 2011-12-07 03:44:41
Well...maybe --这是一个愚蠢的问题,但SqlDatasource SELECT查询中返回的列表中的值实际上是值吗?一个常见的问题是,如果查询中没有空值,那么它就不在列表中,所以一旦切换到编辑模式,就会抛出一个错误,因为默认值为null。
我的库中有一个Misc函数,它接受DropDownList引用并添加一个空行。
编辑以添加一些代码:
我们使用Telerik控件,因此这是针对RadComboBox的,但是由于该控件只是扩展了DropDownList (我认为,它们可能扩展了TextBox.)添加空项的语法应该与下面的类似。
// If there is already an empty row, we will not add an aditional empty row
RadComboBoxItem emptyItem = null;
emptyItem = rcbObject.FindItemByValue(null) ?? rcbObject.FindItemByValue("");
if (emptyItem !=null)
return;
// Add a Empty(null) item to the dropdown list
RadComboBoxItem item_null = new RadComboBoxItem();
item_null.Text = "";
item_null.Value = null;
rcbObject.Items.Insert(0, item_null);希望这能有所帮助!
https://stackoverflow.com/questions/8409815
复制相似问题