• 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 }
    怀揣着一点点梦想的年轻人
    相信技术和创新的力量
    喜欢快速反应的工作节奏
  • 相关阅读:
    云栖大会|盛宴之下,共赴一场视频云的进化论
    云栖大会|感受万物数字化,体验千行视频化
    浅谈语音质量保障:如何测试 RTC 中的音频质量?
    一朵云、一张网、一体化 ——GRTN 打造最佳流媒体场景实践
    穿越时空,跟我一起探索云栖数字谷(2021云栖大会免费送票)
    如何实现 iOS 短视频跨页面的无痕续播?
    如何实现 Android 短视频跨页面的流畅续播?
    揭秘盒马鲜生 Android 短视频秒播优化方案
    SQLServer2012SP1-FullSlipstream-CHS-x64标准版.iso
    mes系统是做什么的?MES是什么全称是什么?
  • 原文地址:https://www.cnblogs.com/hfliyi/p/1964531.html
Copyright © 2020-2023  润新知