detailsview中的dropdownlist:有一个无效 SelectedValue,因为它不在项目列表中
问题是这样的:我有一个产品表,一个类别表,分别如下(已简化):
产品表:Products:id,prodname(产品名),prodcat(产品类别)
类别表:catalogs:id,catalog(类别)
我希望在页面里的detailsview完成产品表的信息输入,其中字段prodcat在进行编辑和插入时,使用dropdownlist,从类别表中取现成的值,在完成编辑后保存时,保存新的值到产品表。
我的操作方法是:
1)拖两个sqldatasource1,sqldatasource2到页面,配制其分别对应产品表和类别表。
2)拖一个detailsview1到页面,配其数据源为sqldatasource1,启用编辑和插入。
3)编辑detailsview1字段,选prodcat(产品类),将其变为模板。
4)编辑模板,选edititemtemplate和insertitemtemplate,均做如下处理:将其原有的textbox删除,拖入dropdownlist1和dropdownlist2,选其数据源为sqldatasource2,启用autopostback。其 DataTextField 和 DataValueField 均为类别表的catalog(类别),再设置dropdownlist1、2的databindings,设其selectedvalue为字段绑定:prodcat。选中双向数据绑定。
5)试运行,发现在点击点击“编辑”功能按钮时,报错:dropdownlist:有一个无效 SelectedValue,因为它不在项目列表中。
我想请大家给我一个完整、标准、可行的方案,解决上述问题,并在更新时,实现新数据保存到产品表中。
--------------------编程问答-------------------- 先清空再赋值,你试试 --------------------编程问答-------------------- 这是因为数据库里的值 不在dropdownlist列表里 所以报告错误 无法指定默认值
最可靠的方法 就是
ddl.SelectedValue=“你的值” 这种写法换成下面的写法:
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(“你的值”)); --------------------编程问答-------------------- --------------------编程问答-------------------- 数据表里有null或空的数据! --------------------编程问答-------------------- dropdownlist没有绑定需要的值,看看页面源码 --------------------编程问答-------------------- 以下是页面源码的有关部分。
<asp:TemplateField HeaderText="catalog" SortExpression="catalog">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("catalog") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1"
DataTextField="catalog"
DataValueField="catalog"
SelectedValue='<%# Bind("catalog") %>'
>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:prodConnectionString1 %>"
SelectCommand="SELECT [id], [catalog] FROM [catalogs]"></asp:SqlDataSource>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1"
DataTextField="catalog"
DataValueField="catalog"
SelectedValue='<%# Bind("catalog") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:prodConnectionString1 %>"
SelectCommand="SELECT [catalog], [id] FROM [catalogs]"></asp:SqlDataSource>
</InsertItemTemplate>
</asp:TemplateField> --------------------编程问答-------------------- if(.Items.FindByValue(value) == null) return; --------------------编程问答--------------------
你说的这些,是在cs文件里还是在aspx文件里的?
在cs文件里的,写到哪个地方?
--------------------编程问答-------------------- 没有解决问题,请帮我顶贴啊。 --------------------编程问答-------------------- 写在你用到 ddl.SelectedValue的前面。
ListItem x = ddl.Items.FindByValue(“你的值”);
if(x!=null)
xxx = ddl.SelectedValue;
else
/无法使用 ddl.SelectedValue 属性 --------------------编程问答-------------------- --------------------编程问答--------------------
首先说声谢谢!
我会在什么地方用dll.selectedvalue?如果我是使用vs对aspx进行设置,而不对cs文件进行编程,那我怎么知道我在哪里会用到dll.selectedvalue?
能详尽点吗?
--------------------编程问答-------------------- 另外
DropdownList的 value不能有重复的
--------------------编程问答-------------------- 肯定没有重复的了。
--------------------编程问答-------------------- 你把你要绑定的列的值取出来
在后台绑定
dropdownlist.Datasource = 你取得的值
dropdownlist.Databind(); --------------------编程问答-------------------- 用((DropDownList)DetailsView1.FindControl("DropDownListID")).SelectedValue.ToString();可以 获得它的selectedvalue值
--------------------编程问答--------------------
写到哪里呢?在cs的哪个事件里写这个? --------------------编程问答-------------------- 居然这是个微软的bug?
A DropDownList Bug
I found there was a bug in System.Web.UI.WebControls.DropDownList recently. When I created items for a DropDownList control using DropDownList.DataBind method, an exception was always thrown. The error message looked like:
'DropDownList_Option' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
This is for sure a bug. I have same issue my website. It worked in 1.1. It does not work in 2.0.
This is a .net 2.0 bug. Microsoft admit it.
<br>I won't show up in .net 3.0. So just wait for .net 3.0
--------------------------------------------------------
But it still shows up in .net 3.5 sp1.
--------------------编程问答-------------------- 楼主到底解决了没有,我也出现了这个问题
补充:.NET技术 , ASP.NET