• 获取gridview模板列里面的控件的ID


    后台 

    public void bind()
        {
            string sqlstr = "select * from t1";
            DataSet myds = constring.Query(sqlstr);
            GridView2.DataSource = myds;
            GridView2.DataBind();
        }

    //翻页

        protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView2.PageIndex = e.NewPageIndex;
            bind();
        }

    //编辑 

    protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
      {
          GridView2.EditIndex = e.NewEditIndex;
          bind();
          ((TextBox)(GridView2.Rows[e.NewEditIndex].Cells[3].FindControl("txtbox1"))).Enabled = true;
          ((TextBox)(GridView2.Rows[e.NewEditIndex].Cells[4].FindControl("txtbox2"))).Enabled = true;
      }

    //更新

      protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {

          string sqlstr = "update tblpsworkingday set Exclusive=" + Convert.ToInt32(((TextBox)(GridView2.Rows[e.RowIndex].Cells[3].FindControl("txtbox1"))).Text.ToString().Trim()) + ",maxqty=" + Convert.ToInt32(((TextBox)(GridView2.Rows[e.RowIndex].Cells[4].FindControl("txtbox2"))).Text.ToString().Trim()) + " where id='" + GridView2.DataKeys[e.RowIndex].Value.ToString() + "'";
          int k = constring.Conn(sqlstr);
          GridView2.EditIndex = -1;
          bind();
      }

    //取消编辑
      protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
      {
          GridView2.EditIndex = -1;
          bind();
      }

    //删除
      protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
      {
          string sqlstr = "delete from tblpsworkingday where id='" + GridView2.DataKeys[e.RowIndex].Value.ToString() + "'";
          int k = constring.Conn(sqlstr);
          bind();
      }

    //前台

    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" CellPadding="4"
      ForeColor="#333333" GridLines="None"  OnRowEditing="GridView2_RowEditing"
      OnRowUpdating="GridView2_RowUpdating" DataKeyNames="id"  OnRowDeleting="GridView2_RowDeleting"  OnRowCancelingEdit="GridView2_RowCancelingEdit"  AllowPaging="True" PageSize="14"  OnPageIndexChanging="GridView2_PageIndexChanging">
      <FooterStyle  Font-Bold="True" ForeColor="White" />
      <Columns>

        <asp:TemplateField HeaderText="单位名称">
                            <ItemTemplate>
                            <asp:Label ID="lbldw1" runat="server" Text=' <%# meinian_site.connec.constring.CutString(Eval("tblCom_Title").ToString(),5)%>' ToolTip='<%# Eval("tblCom_Title").ToString()%>'></asp:Label>    
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="门店名称">
                            <ItemTemplate>
                            <asp:Label ID="lblmd1" runat="server" Text=' <%# meinian_site.connec.constring.CutString(Eval("point_name").ToString(),5)%>' ToolTip='<%# Eval("point_name").ToString()%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                            <asp:TemplateField HeaderText="预约日期">
                            <ItemTemplate>
                                 <%# Eval("date","{0:d}")%>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="是否独占">
                            <ItemTemplate>
                            <asp:TextBox ID="txtbox1" runat="server" Text=' <%# Eval("Exclusive").ToString() %>' Width="30" Enabled="false" ></asp:TextBox>    
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="人数">
                            <ItemTemplate>
                            <asp:TextBox ID="txtbox2" runat="server" Text=' <%# Eval("maxqty").ToString() %>'  Width="30" Enabled="false" ></asp:TextBox>    
                            </ItemTemplate>
                        </asp:TemplateField>

      <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
      <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />

      </Columns>
      <RowStyle BackColor="#EFF3FB"  />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333"  />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#2461BF"  />
                <AlternatingRowStyle BackColor="White" />
      </asp:GridView>

  • 相关阅读:
    vs2019+GLFW+GLAD出现无法解析的外部符号
    图的着色算法
    Head First C# 实验室2(冒险游戏)
    击中和击不中变换
    开运算和闭运算
    膨胀与腐蚀
    两数相加(C#数据结构和算法练习)
    C# 特性和索引(C#学习笔记06)
    C# 索引器(C#学习笔记05)
    C# yield checked,unchecked lock语句(C#学习笔记04)
  • 原文地址:https://www.cnblogs.com/xiaogelove/p/1891151.html
Copyright © 2020-2023  润新知