1、页面代码
1: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
2:
3: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4: <html xmlns="http://www.w3.org/1999/xhtml">
5: <head runat="server">
6: <title></title>
7: </head>
8: <body>
9: <form id="form1" runat="server">
10: <div>
11: </div>
12: <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
13: AutoGenerateColumns="False" DataKeyNames="emp_id" DataSourceID="SqlDataSource1"
14: EmptyDataText="There are no data records to display." Width="466px">
15: <Columns>
16: <asp:CommandField ShowEditButton="True" />
17: <asp:BoundField DataField="emp_id" HeaderText="emp_id" ReadOnly="True" SortExpression="emp_id" />
18: <asp:BoundField DataField="lname" HeaderText="lname" SortExpression="lname" />
19: <asp:BoundField DataField="fname" HeaderText="fname" SortExpression="fname" />
20: <asp:BoundField DataField="minit" HeaderText="minit" SortExpression="minit" />
21: <asp:TemplateField>
22: <ItemTemplate>
23: <asp:DropDownList ID="ddlCity" DataSource='<%#ddlBind() %>' DataValueField="City"
24: DataTextField="City" runat="server">
25: </asp:DropDownList>
26: </ItemTemplate>
27: </asp:TemplateField>
28: </Columns>
29: </asp:GridView>
30: <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
31: DeleteCommand="DELETE FROM [employee] WHERE [emp_id] = @emp_id" InsertCommand="INSERT INTO [employee] ([emp_id], [lname], [fname], [minit]) VALUES (@emp_id, @lname, @fname, @minit)"
32: ProviderName="<%$ ConnectionStrings:pubsConnectionString.ProviderName %>" SelectCommand="SELECT [emp_id], [lname], [fname], [minit] FROM [employee]"
33: UpdateCommand="UPDATE [employee] SET [lname] = @lname, [fname] = @fname, [minit] = @minit WHERE [emp_id] = @emp_id">
34: <DeleteParameters>
35: <asp:Parameter Name="emp_id" Type="String" />
36: </DeleteParameters>
37: <InsertParameters>
38: <asp:Parameter Name="emp_id" Type="String" />
39: <asp:Parameter Name="lname" Type="String" />
40: <asp:Parameter Name="fname" Type="String" />
41: <asp:Parameter Name="minit" Type="String" />
42: </InsertParameters>
43: <UpdateParameters>
44: <asp:Parameter Name="lname" Type="String" />
45: <asp:Parameter Name="fname" Type="String" />
46: <asp:Parameter Name="minit" Type="String" />
47: <asp:Parameter Name="emp_id" Type="String" />
48: </UpdateParameters>
49: </asp:SqlDataSource>
50: </form>
51: </body>
52: </html>
2、执行代码:
1: using System;
2: using System.Data.SqlClient;
3:
4:
5: public partial class Default2 : System.Web.UI.Page
6: {
7: protected void Page_Load(object sender, EventArgs e)
8: {
9:
10: }
11: public SqlDataReader ddlBind()
12: {
13: SqlConnection conn = new SqlConnection("Server=.;Database=pubs;Integrated Security=True");
14: conn.Open();
15: SqlCommand command = new SqlCommand("select distinct city from authors order by city",conn);
16: SqlDataReader reader = command.ExecuteReader();
17: return reader;
18: }
19: }