在Gridview 嵌套DropDownList --Nesting the DropDownList to Gridview in ASP.NET 2.0

本文介绍如何在ASP.NET的GridView控件中使用下拉框进行数据编辑,包括直接绑定数据和创建独立表两种方法,并提及了可以进一步添加搜索功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1、添加Gridview1,配置数据源,我是选择Customers表的CustomerID、CompanyName、ContactName字段。
然后点“高级”选中“生成insert...语句”。

2、选中Gridview1,添加新列,“字段类型”为TemplateField,“页眉文本”为City。然后选中这个新加的列,
点“编辑模板” 在 ItemTemplate里添加个 label控件,选择label控件的“编辑DataBindings”,"可绑定属性"设为Text,"字段绑定,绑定到"设为City,点确定就可以了。

3、在EditItemTemplate添加个DropDownList 控件,选择DropDownList 控件的“编辑DataBindings”,
"可绑定属性"设为SelectedValue,"字段绑定,绑定到"设为City,点确定就可以了。然后点“选择数据源”,“选择数据源”设为SqlDataSource1,“选择要在DropDownList中显示的数据字段”设为City,“为DropDownList的值选择数据字段”设为City。点确定就可以了。

4、大功告成,点编辑就出现下拉框了,可以用于更新了。需要注意到是这里的update是双向的,就是说你把某个客户的city改了,下次下拉框显示的是改后的city:比如ALFKI的city是berlin,而Customers表里只有ALFKI的city是berlin。你把它改为london,下次想把它改回来就找不到berlin了。所以最好如果想把city字段绑定在下拉框,应该建个独立的city的表,这样就Customers表里某人的city不影响到下拉框的显示。

 ==============改进一=========================

DropDownList  不是绑定数据的,而是在代码里自己手动填进去的,Default2.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
            AutoGenerateColumns
="False" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1">
            
<Columns>
                
<asp:CommandField ShowEditButton="True" />
                
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
                
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
                
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
                
<asp:TemplateField HeaderText="City">
                    
<EditItemTemplate>
                        
<asp:DropDownList ID="DropDownList1" runat="server" Width="104px"  SelectedValue='<%# Bind ("City") %>'  >
                                
<asp:ListItem  >Berlin</asp:ListItem>
                                
<asp:ListItem >London</asp:ListItem>
                                
<asp:ListItem >Madrid</asp:ListItem>
                                
<asp:ListItem >FuJian</asp:ListItem>
                                
<asp:ListItem >ShangHai</asp:ListItem>
                                
<asp:ListItem >BeiJing</asp:ListItem>
                        
</asp:DropDownList>
                    
</EditItemTemplate>
                    
<ItemTemplate>
                        
<asp:Label ID="Label1" runat="server" Text='<%# Eval("City") %>' Width="136px"></asp:Label>
                    
</ItemTemplate>
                
</asp:TemplateField>
            
</Columns>
        
</asp:GridView>
        
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            DeleteCommand
="DELETE FROM [Customers] WHERE [CustomerID] = @CustomerID" InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [City], [ContactName]) VALUES (@CustomerID, @CompanyName, @City, @ContactName)"
            SelectCommand
="SELECT [CustomerID], [CompanyName], [City], [ContactName] FROM [Customers]"
            UpdateCommand
="UPDATE [Customers] SET [CompanyName] = @CompanyName, [City] = @City, [ContactName] = @ContactName WHERE [CustomerID] = @CustomerID">
            
<DeleteParameters>
                
<asp:Parameter Name="CustomerID" Type="String" />
            
</DeleteParameters>
            
<UpdateParameters>
                
<asp:Parameter Name="CompanyName" Type="String" />
                
<asp:Parameter Name="City" Type="String" />
                
<asp:Parameter Name="ContactName" Type="String" />
                
<asp:Parameter Name="CustomerID" Type="String" />
            
</UpdateParameters>
            
<InsertParameters>
                
<asp:Parameter Name="CustomerID" Type="String" />
                
<asp:Parameter Name="CompanyName" Type="String" />
                
<asp:Parameter Name="City" Type="String" />
                
<asp:Parameter Name="ContactName" Type="String" />
            
</InsertParameters>
        
</asp:SqlDataSource>
    
    
</div>
    
</form>
</body>
</html>

==============改进二=========================

建个独立的表City,字段只有CityId和City(填写几个记录), DropDownList  绑定到City表的City字段,SqlDataSource2的绑定方式和SqlDataSource1类似,就不说了。Default2.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
            AutoGenerateColumns
="False" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1">
            
<Columns>
                
<asp:CommandField ShowEditButton="True" />
                
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
                
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
                
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
                
<asp:TemplateField HeaderText="City">
                    
<EditItemTemplate>
                        
<asp:DropDownList ID="DropDownList1" runat="server" Width="104px"  SelectedValue='<%# Bind ("City") %>' DataSourceID="SqlDataSource2" DataTextField="City" DataValueField="City" >
                        
</asp:DropDownList>
                        
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
                            SelectCommand
="SELECT [City] FROM [City]">
                         
</asp:SqlDataSource>
                    
</EditItemTemplate>
                    
<ItemTemplate>
                        
<asp:Label ID="Label1" runat="server" Text='<%# Eval("City") %>' Width="136px"></asp:Label>
                    
</ItemTemplate>
                
</asp:TemplateField>
            
</Columns>
        
</asp:GridView>
        
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            DeleteCommand
="DELETE FROM [Customers] WHERE [CustomerID] = @CustomerID" InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [City], [ContactName]) VALUES (@CustomerID, @CompanyName, @City, @ContactName)"
            SelectCommand
="SELECT [CustomerID], [CompanyName], [City], [ContactName] FROM [Customers]"
            UpdateCommand
="UPDATE [Customers] SET [CompanyName] = @CompanyName, [City] = @City, [ContactName] = @ContactName WHERE [CustomerID] = @CustomerID">
            
<DeleteParameters>
                
<asp:Parameter Name="CustomerID" Type="String" />
            
</DeleteParameters>
            
<UpdateParameters>
                
<asp:Parameter Name="CompanyName" Type="String" />
                
<asp:Parameter Name="City" Type="String" />
                
<asp:Parameter Name="ContactName" Type="String" />
                
<asp:Parameter Name="CustomerID" Type="String" />
            
</UpdateParameters>
            
<InsertParameters>
                
<asp:Parameter Name="CustomerID" Type="String" />
                
<asp:Parameter Name="CompanyName" Type="String" />
                
<asp:Parameter Name="City" Type="String" />
                
<asp:Parameter Name="ContactName" Type="String" />
            
</InsertParameters>
        
</asp:SqlDataSource>
    
</div>
    
</form>
</body>
</html>

=================可以把搜索功能添加进来++++++++++=

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值