当前位置: 代码迷 >> ASP.NET >> DetailsView + 模板列控件(Dropdownlist)编辑数据库的有关问题
  详细解决方案

DetailsView + 模板列控件(Dropdownlist)编辑数据库的有关问题

热度:2292   发布时间:2013-02-25 00:00:00.0
DetailsView + 模板列控件(Dropdownlist)编辑数据库的问题
DetailsView1 开启内置的插入、删除、编辑功能。

大部分都是普通的BoundField。
只有一个模板列控件:dropdownlist1。

问题:

如果使用DetailsView控件内置的编辑功能,在更新数据时,
如何得到dropdownlist的值并写回数据库?

------解决方案--------------------------------------------------------
不是找到DropDownList的值的问题。

拿Northwind数据库的Products表和Suppliers表举个例子。
页面上放一个DetailsView+SqlDataSource, SqlDataSource只读取Products表的ProductID, ProductName, SupplierID,生成Insert, Update, Delete.
我们希望编辑状态下SupplierID,在DropDownList里显示Suppliers表的CompanyName,而不是文本框里显示SupplierID.
把SupplierID转换成TemplateField, 然后编辑他的EditItemTemplate,把默认的文本框删掉,放一个DropDownList,给DropDownList另外指定一个SqlDataSource, 读取Suppliers表的SupplierID, CompanyName。然后指定SelectedValue双向绑定到SupplierID字段。
最后生成:

HTML code
<EditItemTemplate>       <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource3"        DataTextField="CompanyName" DataValueField="SupplierID" SelectedValue='<%# Bind("SupplierID") %>'>      </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"       SelectCommand="SELECT [SupplierID], [CompanyName] FROM [Suppliers]"></asp:SqlDataSource></EditItemTemplate>
  相关解决方案