2

针对以下问题发布的解决方案似乎对我不起作用。

如何从gridview Footer c#中的文本框中获取值?

这是我的网格。

      <asp:GridView ID="gridOccupants" runat="server" AllowPaging="True" AutoGenerateColumns="false"
        OnRowCommand="gridOccupants_RowCommand">
        <asp:TemplateField HeaderText="Name" ItemStyle-Width="15px" FooterStyle-Width="15px">
            <asp:TemplateField HeaderText="Id" ItemStyle-Width="15px" FooterStyle-Width="15px">
                <itemtemplate>
                 <asp:Label ID="lblid" runat="server" Text='<%#Bind("id")%>'></asp:Label>
                </itemtemplate>
                <footertemplate>
                 <asp:Button ID="btnInsert" runat="Server" Text="Insert" CommandName="Insert" OnClick="InsertOccupant_Click"
                                    UseSubmitBehavior="False" />
                </footertemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <itemtemplate>
             <asp:Label ID="lblName" runat="server" Text='<%#Bind("name")%>'></asp:Label>
            </itemtemplate>
                <footertemplate>
             <asp:TextBox ID="txtname2" runat="server" Width="50px" > </asp:TextBox>
            </footertemplate>
            </asp:TemplateField>
    </asp:GridView>

    protected void InsertOccupant_Click(object sender, EventArgs e)
    {

            GridViewRow row = gridOccupants.FooterRow;
            string name = ((TextBox)row.FindControl("txtname2")).Text;
    }

此代码不会提取用户在文本框中输入的值 - txtname2。

欢迎任何想法。

4

2 回答 2

2

这可能是因为您正在处理onclick按钮的事件,而不是处理rowcommand您链接到的示例。

试试这个:

  <asp:GridView ID="gridOccupants" runat="server" AllowPaging="True" AutoGenerateColumns="false"
    OnRowCommand="gridOccupants_RowCommand">
    <asp:TemplateField HeaderText="Name" ItemStyle-Width="15px" FooterStyle-Width="15px">
        <asp:TemplateField HeaderText="Id" ItemStyle-Width="15px" FooterStyle-Width="15px">
            <itemtemplate>
             <asp:Label ID="lblid" runat="server" Text='<%#Bind("id")%>'></asp:Label>
            </itemtemplate>
            <footertemplate>
             <asp:Button ID="btnInsert" runat="Server" Text="Insert" CommandName="Insert" UseSubmitBehavior="False" />
            </footertemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <itemtemplate>
         <asp:Label ID="lblName" runat="server" Text='<%#Bind("name")%>'></asp:Label>
        </itemtemplate>
            <footertemplate>
         <asp:TextBox ID="txtname2" runat="server" Width="50px" > </asp:TextBox>
        </footertemplate>
        </asp:TemplateField>
</asp:GridView>

protected void gridOccupants_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("Insert", StringComparison.OrdinalIgnoreCase))
    {
        GridViewRow row = ((GridView)sender).FooterRow;
        TextBox txtname2 = (TextBox)row.FindControl("txtname2");
        if (txtname2 == null)
        {
            return;
        }
        string name = txtname2.Text;
    }
}

在我的脑海中,我不确定它是UseSubmitBehavior="False"从哪里来的<asp:Button>。如果我的示例不起作用,那么您可能需要删除它。

于 2013-07-22T19:59:07.193 回答
0

尝试这个:

protected void gridOccupants_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Insert") 
    {
        var txtFName1 = (TextBox)((GridView)sender).FooterRow.FindControl("txtname2");
        if (txtname2 == null)
        {
            return;
        }
        string name = txtname2.Text;
    }
}
于 2014-11-01T06:22:24.377 回答