ASP.NET2.0中用Gridview控件操作数据
【IT168 技术文档】
首先,我们打算在让用户进行选择,当用户需要新增一记录时,便点击新增按钮,之后在Gridview的最后一行里,显示一个空白行,让用户按字段进行输入,当用户决定不输入新空白记录时,可以按"cancel"按钮返回,该空白行消失。要实现这样的效果,我们可以充分利用Gridview的footer的模版功能进行自定义,因为有3列,所以,在每一列的footer模版中,定义如下:
DataSourceID="SqlDataSource1" DataKeyNames="CustomerID"
AutoGenerateColumns="False" ShowFooter="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="CustomerIDLabel" Runat="Server">
<%# Eval("CustomerID") %></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="CustomerIDTextBox" Runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="CompanyNameLabel" Runat="Server">
<%# Eval("CompanyName") %></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="CompanyNameTextBox" Runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:DropDownList ID="ContactTitleDropDownList"
Runat="server" DataSourceID="SqlDataSource2"
DataTextField="ContactTitle" DataValueField="ContactTitle">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" Runat="server"
SelectCommand="SELECT DISTINCT [ContactTitle] FROM [Customers]"
ConnectionString="server=localhost;uid=sa;password=xxx;database=northwind">
</asp:SqlDataSource>
<asp:Button ID="Button1" Runat="server"
Text="Add" OnClick="Button1_Click" />
<asp:Button ID="CancelButton1" Runat="server"
Text="Cancel" OnClick="CancelButton1_Click" />
</FooterTemplate>
<ItemTemplate>
<asp:DropDownList ID="ContactTitleDropDown"
SelectedValue=’<%# Bind("ContactTitle") %>
’ Runat="Server" DataSourceID="SqlDataSource3"
DataTextField="ContactTitle" DataValueField="ContactTitle" >
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" Runat="server"
SelectCommand="SELECT DISTINCT [ContactTitle] FROM [Customers]"
ConnectionString="server=localhost;uid=sa;password=xxxx;database=northwind"
EnableCaching="True">
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:Gridview>
("ContactTitleDropDownList") as DropDownList;
SqlDataSource1.InsertParameters["CustomerID"].DefaultValue = customerID.Text;
SqlDataSource1.InsertParameters["CompanyName"].DefaultValue = companyName.Text;
SqlDataSource1.InsertParameters["ContactTitle"].DefaultValue=ContactTitle.SelectedValue;
SqlDataSource1.Insert();
}
</script>
[CompanyName], [ContactTitle])
VALUES (@CustomerID, @CompanyName, @ContactTitle)"
SelectCommand="SELECT top 5 [CustomerID], [CompanyName],
[ContactTitle] FROM [Customers]"
ConnectionString="server=localhost;uid=sa;password=XXXXX;database=northwind">
<InsertParameters>
<asp:Parameter Type="String" Name="CustomerID"></asp:Parameter>
<asp:Parameter Type="String" Name="CompanyName"></asp:Parameter>
<asp:Parameter Type="String" Name="ContactTitle"></asp:Parameter>
</InsertParameters>
</asp:SqlDataSource>
<script runat="server"> void Button1_Click(object sender, EventArgs e) { for (int i = 0; i < Gridview1.Rows.Count; i++) { GridviewRow row = Gridview1.Rows[i]; SqlDataSource1.UpdateParameters[0].DefaultValue =
((TextBox)row.Cells[0].FindControl("TextBox2")).Text; SqlDataSource1.UpdateParameters[1].DefaultValue =
((TextBox)row.Cells[1].FindControl("TextBox3")).Text; SqlDataSource1.UpdateParameters[2].DefaultValue =
Gridview1.DataKeys[i].Value.ToString(); SqlDataSource1.Update(); } } </script> <html XMLns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <ASP:Gridview ID="Gridview1" Runat="server"
DataSourceID="SqlDataSource1" DataKeyNames="CustomerID"
AutoGenerateColumns="False"> <Columns> <asp:TemplateField SortExpression="CustomerID"
HeaderText="CustomerID"> <ItemTemplate> <asp:TextBox Runat="server" Text=’<%# Bind("CustomerID") %>
’ ID="TextBox1"></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField SortExpression="CompanyName" HeaderText="CompanyName"> <ItemTemplate> <asp:TextBox Runat="server" Text=’<%# Bind("CompanyName") %>
’ ID="TextBox2"></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField SortExpression="ContactName" HeaderText="ContactTitle"> <ItemTemplate> <asp:TextBox Runat="server" Text=’<%# Bind("ContactTitle") %>
’ ID="TextBox3"></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> </asp:Gridview> <asp:SqlDataSource ID="SqlDataSource1" Runat="server" SelectCommand="SELECT [CustomerID], [CompanyName],
[ContactName], [ContactTitle] FROM [Customers]" UpdateCommand="UPDATE [Customers] SET [CompanyName] =
@CompanyName, [ContactTitle] = @ContactTitle WHERE [CustomerID] = @CustomerID" ConnectionString="server=localhost;uid=sa;password=xxxx;database=northwind"> <UpdateParameters> <asp:Parameter Type="String" Name="CompanyName"></asp:Parameter> <asp:Parameter Type="String" Name="ContactTitle"></asp:Parameter> <asp:Parameter Type="String" Name="CustomerID"></asp:Parameter> </UpdateParameters> </asp:SqlDataSource> <asp:Button ID="Button1" Runat="server" Text="Button" OnClick="Button1_Click" /> </div> </form> </body> </html>
protected void Page_Load(object sender, EventArgs e)
...{
![]()
if (!Page.IsPostBack)
...{
SqlConnection con = new SqlConnection(ConfigurationManager
.ConnectionStrings["AppConnectionString1"].ConnectionString);SqlCommand command = new SqlCommand("SELECT [CustomerID],
[CompanyName], [ContactName], [ContactTitle] FROM [Customers]", con);con.Open();
Gridview1.DataSource = command.ExecuteReader();
Gridview1.DataBind();
con.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
...{
StringBuilder query = new StringBuilder();
for (int i = 0; i < Gridview1.Rows.Count; i++)
...{
GridviewRow row = Gridview1.Rows[i];
string value1 = ((TextBox)row.Cells[0].FindControl("TextBox2"))
.Text.Replace("’", "’’");string value2 = ((TextBox)row.Cells[1].FindControl("TextBox3"))
.Text.Replace("’", "’’");string value3 = Gridview1.DataKeys[i].Value.ToString();
query.Append("UPDATE [Customers] SET [CompanyName] = ’")
.Append(value1).Append("’ , [ContactTitle] = ’").Append(value2).Append("’ WHERE [CustomerID] = ’").Append(value3).Append("’;\n");
}
![]()
SqlConnection con = new SqlConnection(ConfigurationManager
.ConnectionStrings["AppConnectionString1"].ConnectionString);SqlCommand command = new SqlCommand(query.ToString(), con);
con.Open();
command.ExecuteNonQuery();
con.Close();
}
}
<connectionStrings> <add name="NorthwindConnectionString" connectionString=
"Data Source=LIAO;Initial Catalog=Northwind;User ID=sa;
Password=xxxx" providerName="System.Data.SqlClient"/> </connectionStrings>
SqlConnection con = new SqlConnection(ConfigurationManager.
ConnectionStrings["AppConnectionString1"].ConnectionString);
