ASP.NET----表格使用与数据绑定
<body> <form id="form1" runat="server"> <div id="container"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating" OnRowEditing="GridView1_RowEditing" BorderColor="Black" OnRowDataBound="GridView1_RowDataBound" Width="850px" Font-Size="12px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:TemplateField HeaderText="选择"> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="_ID" HeaderText="编号" /> <asp:BoundField DataField="_SerialNumber" HeaderText="订单号" /> <asp:BoundField DataField="_UserTrueName" HeaderText="用户真实姓名" /> <asp:BoundField DataField="_UserPhoneNumber" HeaderText="用户手机号" /> <asp:BoundField DataField="_Receiver" HeaderText="发票接收人" /> <asp:BoundField DataField="_Title" HeaderText="发票抬头" /> <asp:BoundField DataField="_Address" HeaderText="发票地址" /> <asp:BoundField DataField="_ZipCode" HeaderText="邮编" /> <asp:BoundField DataField="_innPrice" HeaderText="对内价格" /> <asp:BoundField DataField="_outPrice" HeaderText="对外价格" /> <asp:CommandField HeaderText="选择" ShowSelectButton="True" /> <asp:CommandField HeaderText="编辑" ShowEditButton="True" /> <asp:CommandField HeaderText="删除" ShowDeleteButton="True" /> <asp:TemplateField HeaderText="订单编辑"> <ItemTemplate> <asp:Button ID="btnEditOrder" runat="server" Text='编 辑' CommandName="EditOrder" /> </ItemTemplate> </asp:TemplateField> </Columns> <HeaderStyle BackColor="Azure" Font-Size="12px" HorizontalAlign="Center" /> <RowStyle HorizontalAlign="Center" /> <PagerStyle HorizontalAlign="Center" /> </asp:GridView> <asp:CheckBox ID="CheckBox2" runat="server" OnCheckedChanged="CheckBox2_CheckedChanged" Text="全选" AutoPostBack="True" /> <asp:Button ID="Button1" runat="server" Height="20px" Text="删 除" OnClick="Button1_Click" /> <asp:Button ID="Button2" runat="server" Height="20px" Text="取 消" OnClick="Button2_Click" /> </div> </form> </body>
后台cs核心代码:
/// <summary> /// 当复选框被点击时发生 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void CheckBox2_CheckedChanged(object sender, EventArgs e) { for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { CheckBox cbox = (CheckBox)(GridView1.Rows[i].FindControl("CheckBox1")); if (CheckBox2.Checked == true) { cbox.Checked = true; } else { cbox.Checked = false; } } } /// <summary> /// 删除所选记录 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button1_Click(object sender, EventArgs e) { for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); if (cbox.Checked == true) { sqlCommon.ExecuteSql("delete from _Grew where ID=" + Convert.ToUInt32(GridView1.DataKeys[i].Value) + ""); } } bind(); } /// <summary> /// 恢复选择 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button2_Click(object sender, EventArgs e) { CheckBox2.Checked = false; for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); cbox.Checked = false; } } /// <summary> /// 在单击 GridView 控件内某一行的 Delete 按钮(其 CommandName 属性设置为"Delete"的按钮)时发生,但在 GridView 控件从数据源删除记录之前。此事件通常用于取消删除操作。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string sqlStr = "delete from _Grew where ID=" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value) + ""; sqlCommon.ExecuteSql(sqlStr); bind(); } /// <summary> /// 在单击 GridView 控件内某一行的 Update 按钮(其 CommandName 属性设置为"Update"的按钮)时发生,但在 GridView 控件更新记录之前。此事件通常用于取消更新操作。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string ID = GridView1.DataKeys[e.RowIndex].Value.ToString(); string I_D = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim(); string Serial_Number = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim(); string User_TrueName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim(); string User_PhoneNumber = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim(); string R_eceiver = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim(); string T_itle = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim(); string A_ddress = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim(); string Z_ipCode = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString().Trim(); string inn_Price = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.ToString().Trim(); string out_Price = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[9].Controls[0])).Text.ToString().Trim(); string sqlStr = "update _Grew set _ID='" + I_D + "',_SerialNumber='" + Serial_Number + "',_UserTrueName='" + User_TrueName + "',_UserPhoneNumber='" + User_PhoneNumber + "',_Receiver='" + R_eceiver + "',_Title='" + T_itle + "',_Address='" + A_ddress + "',_ZipCode='" + Z_ipCode + "',_innPrice='" + inn_Price + "',_outPrice='" + out_Price + "' where ID=" + ID + ""; sqlCommon.ExecuteSql(sqlStr); GridView1.EditIndex = -1; bind(); } /// <summary> /// 在单击 GridView 控件内某一行的 Edit 按钮(其 CommandName 属性设置为“Edit”的按钮)时发生,但在 GridView 控件进入编辑模式之前。此事件通常用于取消编辑操作。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; bind(); } /// <summary> /// 在单击 GridView 控件内某一行的 Cancel 按钮(其 CommandName 属性设置为“Cancel”的按钮)时发生,但在 GridView 控件退出编辑模式之前。此事件通常用于停止取消操作。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; bind(); } /// <summary> /// 在 GridView 控件中的某个行被绑定到一个数据记录时发生。此事件通常用于在某个行被绑定到数据时修改该行的内容。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { foreach (TableCell tc in e.Row.Cells) { tc.Attributes["style"] = "border-color:Black"; } if (e.Row.RowType == DataControlRowType.DataRow) { string OrderID = e.Row.Cells[1].Text; string _jsEdit = "showModalDialog('GridviewEditUpdateDelete.aspx?id=" + OrderID + "',null,'dialogWidth=650px;dialogHeight=500px;help:no;status:no')"; e.Row.Cells[7].Attributes.Add("onclick", _jsEdit); } } /// <summary> /// 数据绑定 /// </summary> public void bind() { string sqlstr = "select * from _Grew"; DataSet myds = sqlCommon.dataSet(sqlstr); GridView1.DataSource = myds; GridView1.DataKeyNames = new string[] { "_ID" }; GridView1.DataBind(); } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { int id = Convert.ToInt32(GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString()); //Response.Write(id); //你的插入数据库新表的代码 } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { switch (e.CommandName) { case "EditOrder": bind(); break; } } }
广积粮,筑高墙,缓称王