C# 获取gridview的页脚行单元格值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18825745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:20:13  来源:igfitidea点击:

Get footer row cell values of gridview

c#asp.netgridviewwebforms

提问by Ruby

I have a gridview with two textboxes in the footer. What's required is get the textbox values, store it to a datatable and then bind the same to the gridview. I am unable to get the textbox values. They show up empty (as you can see). Where am I going wrong.

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

enter image description here

在此处输入图片说明

ASPX:

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:

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;
        }
    }

The textboxes are txtNewId, txtNewName.

文本框是 txtNewId、txtNewName。

采纳答案by reckface

In the button click event, use the following to get the actual GridViewRow of the footerL

在按钮点击事件中,使用如下获取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();
}

EDITThe reason viewstate didn't work is because the viewstate lasts only between postbacks. Sessions stay alive for the duration of the user's session. The default is 20 minutes of idle time.

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

You would normally use ViewState for persisting data between pages during a postback.

您通常会使用 ViewState 在回发期间在页面之间持久化数据。

This doesn't address best practice, it's just what it is.

这并没有解决最佳实践,这就是它的本质。

回答by Sai Avinash

Please check whether you are binding the grid view correctly in the Page Load. What i mean to say is whether you are binding with in the if condidtion

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

       if(!IspostBack)
{

  BindGridView();
}

I hope this helps. Check it out..

我希望这有帮助。一探究竟..

回答by sk2185

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();
             }
    }

回答by Manish Sharma

Use This,

用这个,

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

Or

或者

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

回答by Nisha

To bind the footer with data, use the below code

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

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";
    }
}

...and to retrieve value from the footer textbox,

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

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

回答by Nishant

Do not use grid_Row command for insert. Use button click event

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

like this. It will solve your problem

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

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();
        }
     }
}

回答by RubenP5

I had the same problem and the best way to get the value is using:

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

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

I hope this helps.

我希望这有帮助。