zoukankan      html  css  js  c++  java
  • Gridview模板中编辑的操作

    在Gridview模板中编辑的操作,在正常的现实状态下,应该使用的是:<ItemTemplate></ItemTemplate>模板(非编辑状态),在启动编辑状态时候,使用 <EditItemTemplate></EditItemTemplate>

    ,前台代码如下:

    前台代码
    1 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
    2 onrowcancelingedit="GridView1_RowCancelingEdit"
    3 onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
    4 <Columns>
    5 <asp:TemplateField HeaderText="CustomerID">
    6 <ItemTemplate>
    7 <%#Eval("CustomerID") %>
    8 </ItemTemplate>
    9 <EditItemTemplate>
    10 <%#Eval("CustomerID") %>
    11 </EditItemTemplate>
    12 </asp:TemplateField>
    13 <asp:TemplateField HeaderText="CompanyName">
    14 <ItemTemplate>
    15 <%#Eval("CompanyName") %>
    16 </ItemTemplate>
    17 <EditItemTemplate>
    18 <asp:TextBox ID="TextBox1" Text ='<%#Eval("CompanyName") %>' runat="server"></asp:TextBox>
    19 </EditItemTemplate>
    20 </asp:TemplateField>
    21 <asp:TemplateField HeaderText="ContactName">
    22 <ItemTemplate>
    23 <%#Eval("ContactName") %>
    24 </ItemTemplate>
    25 <EditItemTemplate>
    26 <asp:TextBox ID="TextBox2" runat="server" Text ='<%#Eval("ContactName") %>'></asp:TextBox>
    27 </EditItemTemplate>
    28 </asp:TemplateField>
    29 <asp:TemplateField HeaderText="ContactTitle">
    30 <ItemTemplate>
    31 <%#Eval("ContactTitle") %>
    32 </ItemTemplate>
    33 <EditItemTemplate>
    34 <%#Eval("ContactTitle") %>
    35 </EditItemTemplate>
    36 </asp:TemplateField>
    37 <asp:TemplateField HeaderText="编辑">
    38 <ItemTemplate>
    39 <asp:Button ID="Button1" runat="server" Text="编辑" CommandName="Edit" />
    40 </ItemTemplate>
    41 <EditItemTemplate>
    42 <asp:Button ID="Button2" runat="server" Text="修改" CommandName="Update" />
    43 <asp:Button ID="Button3" runat="server" Text="取消" CommandName="Cancel" />
    44 </EditItemTemplate>
    45 </asp:TemplateField>
    46 </Columns>
    47 </asp:GridView>

    后台代码:

    后台代码
    1 protected void Page_Load(object sender, EventArgs e)
    2 {
    3 if (!IsPostBack)
    4 {
    5 databind();
    6 }
    7 }
    8 public void databind()
    9 {
    10 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
    11 SqlCommand cmd = new SqlCommand();
    12 cmd.Connection = con;
    13 cmd.CommandText = "Select * From Customers";
    14 SqlDataAdapter da = new SqlDataAdapter(cmd);
    15 DataSet ds = new DataSet();
    16 da.Fill(ds);
    17 this.GridView1.DataSource = ds.Tables[0];
    18 this.GridView1.DataKeyNames = new string[] { "CustomerID" };
    19 this.GridView1.DataBind();
    20 }
    21 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    22 {
    23 this.GridView1.EditIndex = e.NewEditIndex;
    24 databind();
    25 }
    26 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    27 {
    28 this.GridView1.EditIndex = -1;
    29 databind();
    30 }
    31 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    32 {
    33 string strKeys = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
    34 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
    35 SqlCommand cmd = new SqlCommand();
    36 cmd.Connection = con;
    37 cmd.CommandText = "Update Customers Set CompanyName =@CompanyName ,ContactName=@ContactName where CustomerID=@CustomerID";
    38 SqlParameter[] sp = new SqlParameter[3];
    39 sp[0] = new SqlParameter("@CompanyName", SqlDbType.NVarChar, 40);
    40 sp[1] = new SqlParameter("@ContactName", SqlDbType.NVarChar, 30);
    41
    42 sp[2] = new SqlParameter("@CustomerID", SqlDbType.NChar, 5);
    43 sp[0].Value = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].FindControl("TextBox1"))).Text.Trim();
    44 sp[1].Value = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[2].FindControl("TextBox2"))).Text.Trim();
    45
    46 sp[2].Value = strKeys;
    47 cmd.Parameters.AddRange(sp);
    48 if (con.State == ConnectionState.Closed)
    49 {
    50 con.Open();
    51 }
    52 cmd.ExecuteNonQuery();
    53 this.GridView1.EditIndex = -1;
    54 databind();
    55
    56 }
    怀揣着一点点梦想的年轻人
    相信技术和创新的力量
    喜欢快速反应的工作节奏
  • 相关阅读:
    EC600S连接阿里云
    纪念首次使用vscode+platformio完成点灯全过程
    使用EC600S-CN实现短信收发功能
    基于stm32,通过更换数据存储扇区提升w25q128flash芯片使用寿命
    0.96寸OLED模块-简述如何修改OLED_ShowChar()函数达到修改显示字体大小的目的
    stm32定时器初始化后自动进入一次中断问题
    个人PSP升级作业
    第一个微信小项目
    自己设计大学排名-数据库实践
    自己的第一个网页
  • 原文地址:https://www.cnblogs.com/hfliyi/p/1964531.html
Copyright © 2011-2022 走看看