当前位置导航:炫浪网>>网络学院>>网页制作>>ASP教程

datagrid编辑、修改、删除、翻页例子

 现在数据库中建立test表,字段分别为ID、Code、Name;

数据库的链接这里就不具体写了,数据库的链接和操作请求我写在了AreaInfo.cs中,代码中会用到,这里声明一下

<%@ Page language="c#" Codebehind="Update.aspx.cs" AutoEventWireup="false" Inherits="Client.Page.AreaInfo.Update" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>test</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 </HEAD>
 <body MS_POSITIONING="GridLayout" bgcolor="ivory">
  <font face="arial" size="3"></font><FONT style="BACKGROUND-COLOR: #ffffff" face="Arial">
  </FONT>
  <br>
  <form runat="server" ID="Form1">
   <TABLE id="Table1" cellSpacing="0" bgcolor="#6699cc" cellPadding="1" width="300" border="1">
    <TR>
     <TD><b>test</b>
     </TD>
    </TR>
     <TD>
      <asp:DataGrid id="DG1" runat="server" BorderColor="green" Width="640" PageSize="5" AllowPaging="true"
       OnPageIndexChanged="Page_Grid" BorderWidth="1" CellPadding="3" AutoGenerateColumns="false"
       ShowHeader="true" Visible="true" OnEditCommand="People_Edit" OnCancelCommand="People_Cancel"
       OnUpdateCommand="People_Update" OnDeleteCommand="People_Delete">
       <HeaderStyle BorderColor="White" BackColor="black" ForeColor="White" Font-Bold="True" Font-Name="Arial"
        Font-Size="9" HorizontalAlign="Center" />
       <ItemStyle BorderColor="" BackColor="#FFFFF0" ForeColor="Black" Font-Name="Arial" Font-Size="8"
        Font-Bold="False" HorizontalAlign="Center" />
       <EditItemStyle BorderColor="" BackColor="#FFFFF0" ForeColor="Black" Font-Name="Arial" Font-Size="7"
        Font-Bold="False" HorizontalAlign="Center" />
       <PagerStyle Mode="NumericPages" Font-Size="8" />
       <Columns>
        <asp:BoundColumn HeaderText="ID" ReadOnly="true" DataField="ID" />
        <asp:TemplateColumn>
         <HeaderTemplate>
          <b>代码</b>
         </HeaderTemplate>
         <ItemTemplate>
          <asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "Code").ToString().Trim() %>' runat="server" ID="Label1" NAME="Label1"/>
         </ItemTemplate>
         <EditItemTemplate>
          <asp:TextBox id="Code" Text='<%# DataBinder.Eval(Container.DataItem, "Code").ToString().Trim() %>' runat="server" Width="100%"/>
         </EditItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn>
         <HeaderTemplate>
          <b>名称</b>
         </HeaderTemplate>
         <ItemTemplate>
          <asp:Label Width="200" Text='<%# DataBinder.Eval(Container.DataItem, "Name").ToString().Trim() %>' runat="server" ID="Label2" NAME="Label2"/>
         </ItemTemplate>
         <EditItemTemplate>
          <asp:TextBox id="Name" Text='<%# DataBinder.Eval(Container.DataItem, "Name").ToString().Trim() %>' runat="server"/>
         </EditItemTemplate>
        </asp:TemplateColumn>
        <asp:EditCommandColumn ButtonType="PushButton" CancelText="取消" EditText="修改" UpdateText="更新" />
        <asp:ButtonColumn Text="删除" CommandName="Delete" ButtonType="PushButton"></asp:ButtonColumn>
       </Columns>
      </asp:DataGrid></TD>
    </TR>
   </TABLE>
  </form>
 </body>
</HTML>

Update.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Client.Page.AreaInfo
{
 /// <summary>
 /// Update 的摘要说明。
 /// </summary>
 public class Update : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.DataGrid DG1;
  protected Classes.AreaInfo myClass=new Client.Classes.AreaInfo();
  private void Page_Load(object sender, System.EventArgs e)
  {
   if(!this.IsPostBack)
   {
    this.DG1.DataSource=myClass.bindGrid();
    this.DG1.DataBind();
   }
   // 在此处放置用户代码以初始化页面
  }
  public void Page_Grid(Object sender, DataGridPageChangedEventArgs e)
  {
   DG1.CurrentPageIndex = e.NewPageIndex;
   DG1.DataSource =myClass.bindGrid();
   DG1.DataBind();

  }
  public void DG1_Edit(Object sender, DataGridCommandEventArgs e)
  {
   DG1.EditItemIndex = e.Item.ItemIndex;
   DG1.DataSource =myClass.bindGrid();
   DG1.DataBind();

  }

  public void DG1_Cancel(Object sender, DataGridCommandEventArgs e)
  {   
   DG1.EditItemIndex = -1;
   DG1.DataSource = myClass.bindGrid();
   DG1.DataBind();

  }

  public void DG1_Update(Object sender, DataGridCommandEventArgs e)
  {
   string code = ((TextBox)e.Item.Cells.Controls).Text;
   string name = ((TextBox)e.Item.Cells.Controls).Text;
   int id=int.Parse(e.Item.Cells[0].Text);
   myClass.upDateArea(id,code,name);
   DG1.EditItemIndex = -1;
   DG1.DataSource = myClass.bindGrid();
   DG1.DataBind();
   

  }

  public void DG1_Delete(Object sender, DataGridCommandEventArgs e)
  {
   int id=int.Parse(e.Item.Cells[0].Text);
   myClass.deleteArea(id);
   this.DG1.DataSource=myClass.bindGrid();
   this.DG1.DataBind();

  }


  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
 }
}

相关内容
赞助商链接