前台代码:
- <asp:GridView ID="GridView1" runat="server" Height="133px" Width="539px"
- AutoGenerateColumns="False" DataKeyNames="id"
- OnRowEditing="GridView1_RowEditing" BackColor="White" BorderColor="White"
- BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1"
- GridLines="None" OnRowUpdating="GridView1_RowUpdating"
- OnRowCancelingEdit="GridView1_RowCancelingEdit"
- OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting" >
- <Columns>
- <asp:BoundField DataField="channel" HeaderText="频道名称" />
- <asp:BoundField DataField="id" HeaderText="编号" DataFormatString="{0:000#}" ReadOnly="True" />
- <asp:BoundField DataField="descript" HeaderText="频道描述" />
- <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
- <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
- </Columns>
- <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
- <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
- <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
- <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
- <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
- </asp:GridView>
后台代码:
- protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
- {
- GridView1.EditIndex = e.NewEditIndex;
- BindDataList();
- }
- protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
- {
- this.GridView1.EditIndex = -1;
- BindDataList();
- }
- protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
- {
- GridViewRow row = GridView1.Rows[e.RowIndex];
- string ID = GridView1.DataKeys[e.RowIndex].Values[0].ToString().Trim();
- NJ.Internet.NJ_Channel_Dal ncd = new NJ.Internet.NJ_Channel_Dal();
- int count = ncd.DelChannel(Convert.ToInt32(ID));
- if (count != 0)
- {
- Response.Write("<script type='text/javascript'>window.alert('删除成功')</script>");
- BindDataList();
- }
- }
- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
- {
- }
- protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
- {
- try
- {
- GridViewRow row = GridView1.Rows[e.RowIndex];
- string Name = ((TextBox)(row.Cells[0].Controls[0])).Text;
- string Id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
- string Descript = ((TextBox)(row.Cells[2].Controls[0])).Text.Trim();
- NJ.Internet.NJ_Channel_Dal ncd = new NJ.Internet.NJ_Channel_Dal();
- if (!String.IsNullOrEmpty(Id))
- {
- int count = ncd.UptChannel(Convert.ToInt32(Id), Name, Descript);
- if (count != 0)
- {
- GridView1.EditIndex = -1;
- BindDataList();
- }
- }
- }
- catch (Exception ee)
- {
- Response.Write("<scrit>alert('" + ee.Message + "')</script>");
- }
- }
这实现了更新,删除还没有实现。希望大家能够参考,如果有错误请指出,大家共同进步
转载于:https://blog.51cto.com/liguo125/508985