页面代码Default.aspx GridView 部分
<asp:GridView ID="gvMain" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource2" Height="144px" CellPadding="0" ForeColor="#333333"
GridLines="None" OnRowDataBound="gvMain_RowDataBound"
HorizontalAlign="Center">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input id="chbCheckAll" οnclick="GetAllCheckBox(this)" type="checkbox" />全选
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chbCheck" runat="server" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="100px" />
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="编号" SortExpression="Id">
<ItemStyle HorizontalAlign="Center" Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="Title" HeaderText="标题" SortExpression="Title">
<ItemStyle Width="300px" />
</asp:BoundField>
<asp:BoundField DataField="Author" HeaderText="作者" SortExpression="Author">
<ItemStyle Width="300px" />
</asp:BoundField>
<asp:TemplateField>
<HeaderTemplate>
类别
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Category.Name") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:CommandField ShowEditButton="True">
<ItemStyle Width="100px" />
</asp:CommandField>
</Columns>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:DropDownList ID="ddlCategory" runat="server" DataTextField="Name" DataValueField="Id"
Height="20px" Width="144px" DataSourceID="ObjectDataSource1">
</asp:DropDownList>
<asp:Button ID="btnModify" runat="server" OnClick="btnModify_Click" Text=" 修 改" />
后置代码Default.aspx.cs
protected void btnModify_Click(object sender, EventArgs e)
{
用foreach循环
if (gvMain.Rows.Count > 0)
{
foreach (GridViewRow rows in gvMain.Rows)
{
CheckBox chbSelected = (CheckBox)rows.Cells[0].FindControl("chbCheck");
if (chbSelected.Checked)
{
//调用修改的方法
BookManager.UpdateBookCategoryById(Convert.ToInt32(ddlCategory.SelectedItem.Value), Convert.ToInt32(rows.Cells[1].Text)); //ddlCategory.SelectedItem.Value,是获取DropDownList的值
//rows.Cells[1].Text,获取的是GridView中编号那一列的值
}
}
用for循环
//for(int i=0;i<this.gvMain.Rows.Count;i++)
//{
// CheckBox chbSelected = (this.gvMain.Rows[i].Cells[0].FindControl("chbCheck")) as CheckBox;
// if (chbSelected.Checked == true)
// {
// BookManager.UpdateBookCategoryById(Convert.ToInt32(ddlCategory.SelectedItem.Value), Convert.ToInt32(this.gvMain.Rows[i].Cells[0].Text.ToString()));
// }
//}
Response.Redirect("Default.aspx");
}
业务逻辑层BookManager
public static int UpdateBookCategoryById(int categoryId, int Id)
{
return BookDAL.UpdateBookCategoryById(categoryId, Id);
}
数据访问层BookDAL
public static int UpdateBookCategoryById(int categoryId, int Id)
{
string connString="server=ZHANGYUHONG;database=BookShop;uid=sa;pwd=sa";
string sql = string.Format("update Books set CategoryId={0} where Id={1}", categoryId, Id);
SqlConnection connection =new SqlConnection(connString);
SqlCommand command=new SqlCommand(sql,connection);
connection.Open();
int count = command.ExecuteCommand(sql);
connection.Close();
return count;
}