DataGrid编辑更新取消(20070512)

来源:互联网 发布:淘宝发货能改地址吗 编辑:程序博客网 时间:2024/04/19 14:44

*****************前台代码*******************************
<%@ Page language="c#" Codebehind="employee_admin.aspx.cs" AutoEventWireup="false" Inherits="softweb.employee.employee_admin" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>employee_admin</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body background="../images/rose_v5.gif" MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <table cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
    <tr align="center">
     <td align="center"><asp:label id="Label1" runat="server" Width="288px">员&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;工&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;维&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;护</asp:label></td>
    </tr>
    <tr align="center">
     <td vAlign="top" align="center"><asp:datagrid id="DataGrid1" runat="server" Width="790px" Height="192px"
       AutoGenerateColumns="False" DataKeyField="employeeID">
       <ItemStyle HorizontalAlign="Center"></ItemStyle>
       <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
       <Columns>
        <asp:BoundColumn Visible="False" DataField="employeeID" HeaderText="id"></asp:BoundColumn>
        <asp:TemplateColumn HeaderText="序 号">
         <ItemTemplate>
          <%# xuhao++%>
         </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderText="员工姓名">
         <ItemTemplate>
          <%# DataBinder.Eval(Container.DataItem,"employee_name")%>
         </ItemTemplate>
         <EditItemTemplate>
          <FONT face="宋体">
           <asp:TextBox id="txtName" runat="server">sss</asp:TextBox></FONT>
         </EditItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderText="性    别">
         <ItemTemplate>
          <%# DataBinder.Eval(Container.DataItem,"sex","{0:c}")%>
         </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderText="用 户 名">
         <ItemTemplate>
          <%# DataBinder.Eval(Container.DataItem,"user_name")%>
         </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderText="入职时间">
         <ItemTemplate>
          <%# DataBinder.Eval(Container.DataItem,"in_age")%>
         </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderText="权    限">
         <ItemTemplate>
          <%# DataBinder.Eval(Container.DataItem,"quanxian")%>
         </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderText="密    码">
         <ItemTemplate>
          <%# DataBinder.Eval(Container.DataItem,"pwd")%>
         </ItemTemplate>
        </asp:TemplateColumn>
        <asp:EditCommandColumn ButtonType="PushButton" UpdateText="更  新" HeaderText="编  辑" CancelText="取  消" EditText="修 改"></asp:EditCommandColumn>
        <asp:ButtonColumn Text="删 除" ButtonType="PushButton" CommandName="Delete"></asp:ButtonColumn>
        <asp:BoundColumn DataField="employee_name" HeaderText="test"></asp:BoundColumn>
       </Columns>
      </asp:datagrid></td>
    </tr>
   </table>
  </form>
 </body>
</HTML>
**********************后台代码************************************
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;
using System.Data.SqlClient;

namespace softweb.employee
{
 /// <summary>
 /// employee_admin 的摘要说明。
 /// </summary>
 public class employee_admin : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  protected SqlConnection myconn;
  protected int xuhao;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!this.IsPostBack)
   {
    this.bind();//调用绑定函数
   }
   /*
    *
    *
    * */
   
  }
  private void bind()
  {
   //数据库连接
   DataView dv=new DataView();//定义数据表视图
   xuhao=1;//自动序号排列
   myconn=new SqlConnection(Session["strConn"].ToString());
   myconn.Open();
   SqlCommand cmd=new SqlCommand();
   cmd.CommandType=CommandType.StoredProcedure;//查询语句为存储过程
   cmd.CommandText="employee_view";
   cmd.Connection=myconn;
   SqlDataAdapter adpt=new SqlDataAdapter(cmd);
   DataSet ds=new DataSet();
   adpt.Fill(ds,"tab");
   myconn.Close();
   dv=ds.Tables["tab"].DefaultView;
   this.DataGrid1.DataSource=dv;//调用绑定函数
   this.DataGrid1.DataBind();
   //return dv; //返回一个数据表视图
   
   
  }

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

  }
  #endregion

  private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
  {
  
  }

  private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   this.DataGrid1.EditItemIndex=-1;
   this.bind();
  }

  private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
  
  }

  private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
   this.bind();
  }

  private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
 
  /*
   
  在DataGrid控件中,一般只是在绑定列或模版列中进行数据显示。
在编写DataGrid控件(控件名为dg1)的事件的处理方法时,通常希望获取数据项某列的数据。
  除了SelectedInexChanged事件外,其它事件的处理方法的参数e都是DataGridCommandEventArgs类的实例。
 通过e实例,可以获取引发事件的数据项中数据。 1.获取绑定列中的数据
 
 1)处于非编辑状态、或者数据项处于编辑状态但该绑定列只读
  e.Item.Cells[列索引].Text
 2)处于编辑状态
  绑定列如果处于编辑状态,则会显示为TextBox控件形式 ((TextBox)(e.Item.Cells[列索引].Controls[0])).Text
2.获取模版列中的数据 。在模版列中可以放置一个或多个控件(右键"编辑模版"菜单),这些控件可以绑定数据源中的数据
 要获取绑定到模版列中某控件上的数据,首先要知道该控件的id,然后用FindControl方法获得该控件的引用。
 ((控件类型)(e.Item.Cells[列索引].FindControl("控件id"))).控件的属性
 eg:((CheckBox)(e.Item.Cells[3].FindControl("cbSex"))).Checked  
 
  */
   

  
   try
   {
    myconn=new SqlConnection(Session["strConn"].ToString());
    myconn.Open();
    //取出修改后的数据
    string str=((TextBox)(e.Item.Cells[2].FindControl("txtName"))).Text;
    //Response.Write(str+"<br>");
    string str1=((TextBox)(e.Item.Cells[10].Controls[0])).Text;
    //Response.Write(str1);
    int id=(int)this.DataGrid1.DataKeys[(int)e.Item.ItemIndex];
    //Response.Write("<script>alert('"+id+"');</script>");
    string sqlstr;
    sqlstr="update employee set employee_name='"+str.Trim()+"'where employeeID="+id;
    SqlCommand cmd=new SqlCommand( sqlstr,myconn);
    cmd.ExecuteNonQuery();
    myconn.Close();
   }
  catch(Exception ex)
   {
    this.RegisterStartupScript("ex","<script>alert('"+ex.Message.ToString()+"');</script>");
   }
    //不管是否抛出异常,都执行finally语句
   finally
   {
    this.RegisterStartupScript("sueecssedit","<script>alert('数据修改成功');</script>");
    this.DataGrid1.EditItemIndex=-1;
    this.bind();
   }
   
   
  }
 }
}
 

原创粉丝点击