简体   繁体   English

如何从ASP GridView页脚获取所有值

[英]How to Get All the Values from a ASP gridview footer

I have been fighting this problem all day. 我整天都在解决这个问题。 I have a asp:Gridview with several footer Templates that has several TextBox fields, a DropDown and a CheckBox. 我有一个带几个页脚模板的asp:Gridview ,其中有几个TextBox字段,一个DropDown和一个CheckBox。 I hide the footer until the add records asp:button is clicked. 我隐藏页脚,直到单击添加记录asp:button That all works and I can enter data in the text boxes, chose elections from the drop down and cycle the checkbox. 一切正常,我可以在文本框中输入数据,从下拉列表中选择选举,然后循环复选框。 Now when I try to save the data to the database I use this code. 现在,当我尝试将数据保存到数据库时,我将使用此代码。

protected void MembersGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("EmptyInsert"))
    {

    }

    if (e.CommandName.Equals("Insert"))
    {

        int Role =Convert.ToInt32((MembersGridView.FooterRow.FindControl("ddlRole") as DropDownList).SelectedItem.Value);
        string FirstName = (MembersGridView.FooterRow.FindControl("txtLastName") as TextBox).Text;
        string LastName = (MembersGridView.FooterRow.FindControl("txtLastName") as TextBox).Text;
        TextBox UserName = MembersGridView.FooterRow.FindControl("txtUserName") as TextBox;
        TextBox Email = MembersGridView.FooterRow.FindControl("txtEmail") as TextBox;
        CheckBox active = MembersGridView.FooterRow.FindControl("chkActive") as CheckBox;
        DbContext.AAHMembersAdd(Role, FirstName, LastName, UserName.Text, Email.Text, active.Checked);

        //MembersGridView.EditIndex = -1;

        MembersGridView.DataSource = DbContext.VW_Members.ToList(); 
        DataBind();
    }
}

<asp:TemplateField HeaderText="First Name" ItemStyle-Width="100">
   <ItemTemplate>
        <asp:Label ID="lblFirstName" runat="server" Text='<%# Eval("firstname")%>'></asp:Label>
   </ItemTemplate>
   <EditItemTemplate>
        <asp:TextBox ID="txtFirstName" runat="server" Text='<%# Eval("firstname")%>'></asp:TextBox>
   </EditItemTemplate>
   <FooterTemplate>
       <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
   </FooterTemplate>
</asp:TemplateField>

I have added the aspx for one of the Template fields all are the same as this one except the drop down and CheckBox 我为模板字段之一添加了aspx,除了下拉列表和CheckBox之外,其他都与此模板相同

The DropDown and the CheckBox data is returned however all of my TextBox values are returned as an empty string. 返回了DropDown和CheckBox数据,但是我所有的TextBox值都作为一个空字符串返回。 What am I missing here. 我在这里想念什么。 As you can see I have already tries a couple of different ways but the results are the same. 如您所见,我已经尝试了几种不同的方法,但是结果是相同的。

From what I can see; 据我所见; you are not setting the text to anything - therefore the textbox.text value is an empty string. 您没有将文本设置为任何内容-因此textbox.text值是一个空字符串。 Try changing this code: 尝试更改此代码:

<FooterTemplate> <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox> </FooterTemplate>

to this: 对此:

<FooterTemplate> <asp:TextBox ID="txtFirstName" text="hello" runat="server"></asp:TextBox> </FooterTemplate>

also change this: 也改变这个:

string FirstName = (MembersGridView.FooterRow.FindControl("txtLastName") as TextBox).Text;

to this: 对此:

 string FirstName = (MembersGridView.FooterRow.FindControl("txtFirstName") as TextBox).Text;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM