在平時的實際項目中,為了減少往返提交數據,可以一次編輯 GridView 的所有行,這樣大大提高用戶使用產品的體驗。下面就講解何一次編輯 GridVIEw 的所有數據行。在下面的例子中,數據庫使用《ASP.Net 2.0應用開發技術》一書中Site\App_Data\下的 ASPNET20Book.mdb 數據庫。例子中的數據更新方式使用 SqlDataSource 數據源控件的UpdateCommand屬性,但此方法同樣適用於存儲過程,SQL 語句等。
C#
<%@ Page Language="C#" Debug="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xHtml1-transitional.dtd">
<script runat="server">
string[] ClassNameArray = { "大學", "中學", "高中" };
//初始化原來的值
protected void GridView1_RowDataBound(object sender, GridVIEwRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
System.Data.DataRowView drv = (System.Data.DataRowVIEw)e.Row.DataItem;
RadioButtonList rbl = (RadioButtonList)e.Row.FindControl("txtGender");
if (rbl != null)
{
if ((bool)drv["Gender"])
{
rbl.Items.FindByText("男").Selected = true;
}
else
{
rbl.Items.FindByText("女").Selected = true;
}
}
DropDownList ddl = (DropDownList)e.Row.FindControl("txtClassName");
if (ddl != null)
{
ddl.Items.FindByText(drv["ClassName"].ToString()).Selected = true;
}
}
}
// 初始化項目列表
protected void GridView1_RowCreated(object sender, GridVIEwRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("txtClassName");
ddl.DataSource = ClassNameArray;
ddl.DataBind();
}
}
//處理提交的數據
protected void XianhuiMeng_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in GridVIEw1.Rows)
{
string tGender = ((RadioButtonList)gvr.FindControl("txtGender")).SelectedValue;
SqlDataSource1.UpdateParameters.Add(new Parameter("@Title", TypeCode.String, ((TextBox)gvr.FindControl("txtTitle")).Text));
SqlDataSource1.UpdateParameters.Add(new Parameter("@Gender", TypeCode.Boolean, (tGender == "男" ? "True" : "False")));
SqlDataSource1.UpdateParameters.Add(new Parameter("@ClassName",
TypeCode.String, ((DropDownList)gvr.FindControl("txtClassName")).SelectedValue));
SqlDataSource1.UpdateParameters.Add(new Parameter("@id", TypeCode.Int32, GridVIEw1.DataKeys[gvr.DataItemIndex].Value.ToString()));
SqlDataSource1.Update();
SqlDataSource1.UpdateParameters.Clear();
}
Response.Redirect(Request.UrlReferrer.ToString());
}
</script>
<html XMLns="http://www.w3.org/1999/xHtml">
<head id="Head1" runat="server">
<title>同時編輯 GridVIEw的 多行</title>
</head>
<body>
<form id="form1" runat="server">
<ASP:GridView ID="GridVIEw1" runat="server" Font-Size="12px" CellPadding="3" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1" OnRowDataBound="GridVIEw1_RowDataBound"
OnRowCreated="GridVIEw1_RowCreated">
<HeaderStyle BackColor="#EDEDED" />
<Columns>
<ASP:TemplateFIEld HeaderText="姓名">
<ItemTemplate>
<asp:TextBox ID="txtTitle" runat="server" Text='<%#Eval("Title") %>'></ASP:TextBox>
</ItemTemplate>
</ASP:TemplateFIEld>
<ASP:TemplateFIEld HeaderText="性別">
<ItemTemplate>
<ASP:RadioButtonList ID="txtGender" runat="server" RepeatColumns="2">
<asp:ListItem Text='男'></ASP:ListItem>
<asp:ListItem Text='女'></ASP:ListItem>
</ASP:RadioButtonList>
</ItemTemplate>
</ASP:TemplateFIEld>
<ASP:TemplateFIEld HeaderText="學歷">
<ItemTemplate>
<ASP:DropDownList ID="txtClassName" runat="server">
</ASP:DropDownList>
</ItemTemplate>
</ASP:TemplateFIEld>
</Columns>
</ASP:GridVIEw>
<ASP:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\ASPNET20Book.mdb;Persist Security Info=True"
ProviderName="System.Data.OleDb" SelectCommand="SELECT * FROM [Student]"
UpdateCommand="Update [Student] Set Title = @Title,Gender = @Gender,ClassName=@ClassName Where id=@id"></ASP:SqlDataSource>
<ASP:Button ID="Button1" runat="server" Text="保存所有修改" OnClick="XianhuiMeng_Click" />
</form>
</body>
</Html>
注意:對於SQL Server 數據庫,參數名字不加@。另外注意:不同數據庫類型和不同的數據源,UpdateCommand="Update [Student] Set Title = @Title,Gender = @Gender,ClassName=@ClassName Where id=@id"寫法和SqlDataSource1.UpdateParameters.Add(new Parameter("@Title", TypeCode.String, ((TextBox)gvr.FindControl("txtTitle")).Text)); 的寫法會有所區別。