【问题标题】:Get footer row cell values of gridview获取gridview的页脚行单元格值
【发布时间】:2013-09-20 11:15:07
【问题描述】:

我有一个gridview,页脚中有两个文本框。所需的是获取文本框值,将其存储到数据表中,然后将其绑定到网格视图。 我无法获取文本框值。它们显示为空(如您所见)。我哪里错了。

ASPX:

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" 
ShowFooter="true" OnRowDataBound="gv_RowDataBound"  
OnRowCommand="gv_RowCommand">           
 <Columns>
  <asp:TemplateField>
   <ItemTemplate>
    <asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit">
</asp:LinkButton>
  </ItemTemplate>
  <EditItemTemplate>
   <asp:LinkButton ID="lnkUpdate" runat="server" Text="Update" 
CommandName="Update"></asp:LinkButton>
   &nbsp;<asp:LinkButton ID="lnkCancel" runat="server" Text="Cancel"  
   CommandName="Cancel"></asp:LinkButton>
   </EditItemTemplate>
   </asp:TemplateField>
   <asp:TemplateField HeaderText="S.No">
     <ItemTemplate>
       <%#Container.DataItemIndex %>
     </ItemTemplate>
     </asp:TemplateField>
     <asp:TemplateField HeaderText="ID">
       <ItemTemplate>
       <asp:Label ID="lbId" runat="server" Text='<%#Eval("id") %>'></asp:Label>
       </ItemTemplate>
    <EditItemTemplate>
     <asp:TextBox ID="txtId" runat="server" Text='<%#Eval("id") %>'>
     </asp:TextBox>
     </EditItemTemplate>
     <FooterTemplate>
     <asp:TextBox ID="txtNewId" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator runat="server" ControlToValidate="txtNewId" 
     SetFocusOnError="true"
    ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
    </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="NAME">
    <ItemTemplate>
    <asp:Label ID="lbName" runat="server" Text='<%#Eval("name") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
     <asp:TextBox ID="txtName" runat="server" Text='<%#Eval("name") %>'>
     </asp:TextBox>
      </EditItemTemplate>
      <FooterTemplate>
     <asp:TextBox ID="txtNewName" runat="server"></asp:TextBox>
     <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
      ControlToValidate="txtNewName" SetFocusOnError="true" 
      ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
     </FooterTemplate>
     </asp:TemplateField>
     <asp:TemplateField>
     <ItemTemplate>
     <asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" 
      CommandName="Delete"></asp:LinkButton>
     </ItemTemplate>
     <FooterTemplate>
     <asp:LinkButton ID="lnkInsert" runat="server" Text="Insert" 
       CommandName="Insert" ></asp:LinkButton>                    
     </FooterTemplate>
     </asp:TemplateField>
     </Columns>
    </asp:GridView>

CS:

protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        DataTable dt = new DataTable();

        switch (e.CommandName)
        {
            case "Insert":
                GridViewRow fRow = gv.FooterRow;
                dt.Columns.Add("id");
                dt.Columns.Add("name");
                dt = (DataTable)ViewState["students"];
                DataRow dr = dt.NewRow();
                dr["id"] = ((TextBox)fRow.FindControl("txtNewId")).Text;
                dr["name"] = ((TextBox)fRow.FindControl("txtNewName")).Text;
                dt.Rows.Add(dr);
                ViewState["students"] = dt;
                gv.DataSource = ViewState["students"];
                gv.DataBind();
                break;
        }
    }

文本框是 txtNewId、txtNewName。

【问题讨论】:

  • 请显示 GridView 标记
  • 如果以下任何答案是您所期待的,您必须将其标记为答案。这可以帮助遇到此问题的其他人快速确定最佳答案
  • 那么……那赏金呢?
  • 谢谢大家。不幸的是,没有一个解决方案奏效。我已经用 ASPX 代码更新了这个问题。另外,为什么我不应该使用链接按钮而是使用按钮?

标签: c# asp.net gridview webforms


【解决方案1】:

我遇到了同样的问题,获得价值的最佳方法是使用:

((TextBox)<grid_name>.FooterRow.FindControl("<textBox_id>")).Text

我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    在按钮点击事件中,使用如下获取footerL的实际GridViewRow

    protected void insertButton_Click(object sender, EventArgs e)
    {
        // This is the crux - 
        GridViewRow row = (GridViewRow)((sender as Button).NamingContainer);
        // ...
        // then you can get your textboxes
        // Since we know it's an insert
        dt.Columns.Add("id");
        dt.Columns.Add("name");
        dt = (DataTable)Session["students"];
        DataRow dr = dt.NewRow();
        TextBox txtnewid = (TextBox) row.FindControl("txtNewId");
        TextBox txtnewName =  (TextBox) row.FindControl("txtNewName");
        dr["id"] =  txtnewid.Text;
        dr["name"] = txtnewName.Text ;
        dt.Rows.Add(dr);
        Session["students"] = dt;
        gv.DataSource = dt;
        gv.DataBind();
    }
    

    编辑 视图状态不起作用的原因是视图状态仅在回发之间持续。会话在用户会话期间保持活动状态。默认为 20 分钟的空闲时间。

    您通常会使用 ViewState 在回发期间在页面之间保存数据。

    这不是最佳实践,它就是这样。

    【讨论】:

    • 没有错误,但是点击按钮,它添加了一个空行
    • 这部分只是你的代码的粘贴。关键是你没有得到页脚行,现在你得到了。如果您得到一个空行,则表明您没有填充文本框。我想授予赏金说起来容易做起来难。
    • 哦,不要将数据表存储在视图状态中。将其存储在会话中。
    • @reckface...非常感谢。它与会话一起工作。你能告诉我为什么会这样吗?是因为我在 web.config 中有一些会话配置。再次感谢您
    【解决方案3】:

    不要使用 grid_Row 命令进行插入。使用按钮点击事件

    像这样。它会解决你的问题

    protected void OnCmdInsertClick(object sender, EventArgs e)
    {
        //Grid's footer row
        var footerRow = gv.FooterRow;
        if(footerRow !=null)
        {
            //get your textbox instances
            var txtNewId = (TextBox) footerRow.FindControl("txtNewId");
            var txtNewName = (TextBox) footerRow.FindControl("txtNewName");
            // Check for null
            if(txtNewId !=null && txtNewName !=null)
            {
                var dt = (DataTable)ViewState["students"];
                DataRow dr = dt.NewRow();
                dr["id"] =  txtNewId.Text;
                dr["name"] = txtNewName.Text;
                dt.Rows.Add(dr);                
                ViewState["students"] = dt;
                gv.DataSource = ViewState["students"];
                gv.DataBind();
            }
         }
    }
    

    【讨论】:

    • 1000 票给你。谢谢你节省我的时间老兄:)
    【解决方案4】:
    protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            DataTable dt = new DataTable();
    
       if (e.CommandName.Equals("Insert"))
                 {
                    GridViewRow fRow = gv.FooterRow;
                    dt.Columns.Add("id");
                    dt.Columns.Add("name");
                    dt = (DataTable)ViewState["students"];
                    DataRow dr = dt.NewRow();
                    TextBox txtnewid = (TextBox) fRow.FindControl("txtNewId");
                    TextBox txtnewName =  (TextBox) fRow.FindControl("txtNewName");
                    dr["id"] =  txtnewid.Text;
                    dr["name"] = txtnewName.Text ;
                    dt.Rows.Add(dr);
                    ViewState["students"] = dt;
                    gv.DataSource = ViewState["students"];
                    gv.DataBind();
                 }
        }
    

    【讨论】:

      【解决方案5】:

      要将页脚与数据绑定,请使用以下代码

      protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.Footer)
          {
               TextBox txtNewId = (TextBox)e.Row.FindControl("txtNewId");
               txtNewId.Text = "New 01";
          }
      }
      

      ...并从页脚文本框中检索值,

      TextBox txtNewId = (TextBox)gvGrid.FooterRow.FindControl("txtNewId");
      

      【讨论】:

        【解决方案6】:

        使用这个,

        TextBox txtName = GridView1.FooterRow.FindControl("yourtextboxId") as TextBox;
        string name = txtName.Text;
        

        或者

        GridViewRow row = ((GridView)sender).FooterRow;
        TextBox txtName = (TextBox)row.FindControl("yourtextboxId");
        if (txtName == null)
        {
            return;
        }
        string name = txtName.Text;
        

        【讨论】:

        • 尝试了这两种解决方案。没有运气。
        【解决方案7】:

        请检查您是否在页面加载中正确绑定了网格视图。我的意思是你是否在 if 条件下具有约束力

               if(!IspostBack)
        {
        
          BindGridView();
        }
        

        我希望这会有所帮助。看看吧。。

        【讨论】:

        • 是的,函数在 if 块中
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-02
        • 1970-01-01
        • 2021-06-28
        相关资源
        最近更新 更多