0

我在使用 Web 应用程序时遇到问题。我有一个与 SQL 数据表连接的 gridview,在页脚中我有文本框(或下拉列表)和一个“插入”按钮。当我单击按钮并想在我的 SQL 表中插入新行时,就会出现问题。它不会从文本框中读取文本(我们需要输入一些字符串)。当然插入不会发生,因为某些数据不能为空。我对编辑行有同样的问题。

这是我的代码:

网格视图:

<asp:GridView ID="gvUser" runat="server" AutoGenerateColumns="False" 
DataKeyNames="ID" OnRowDataBound="gvUser_RowDataBound"
OnRowCommand="gvUser_RowCommand" ShowFooter="True">
    <Columns>
        <asp:TemplateField HeaderText="ID">
            <ItemTemplate>
                <asp:Label ID="lblID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="lblName" runat="server" Text='<%# Bind("name") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtNewName" runat="server"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Last name">
            <ItemTemplate>
                <asp:Label ID="lblLname" runat="server" Text='<%# Bind("lname") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtLname" runat="server" Text='<%# Bind("lname") %>'></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtNewLname" runat="server"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Edit" ShowHeader="False">
            <FooterTemplate>
                <asp:LinkButton ID="lnkAdd" runat="server" CausesValidation="False" CommandName="Insert"
                    Text="Shrani"></asp:LinkButton>
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

后面的代码:

protected void BindGV()
{
    //bind the SQL table to the GridView (no problem here)
}

protected void gvUser_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //here I bind the dropdownlist (did not include it in code snippet, 
    //since firstly I need to get text from textboxes
}

protected void gvUser_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("Insert"))
    {
        TextBox txtNewName = (TextBox)gvUser.FooterRow.FindControl("txtNewName");
        TextBox txtNewLname = (TextBox)gvUser.FooterRow.FindControl("txtNewLname");

        string NewName = txtNewName.Text; //this strings come up empty (just "")
        string NewLname = txtNewLname.Text; //it should read from textboxes

        AddRow(NewName, NewLname);

        BindGV();
    }
}

private void AddRow(string name, string lname)
{
    //insert row into SQL datatable
}

编辑:现在可以了。发现了一个类似的问题,作者说他能够通过将 EnableViewState="false" 添加到 Gridview 来解决它。

我试过了,它奏效了。:)

这里的任何人都能够回答为什么这会起作用?这将如何与其他 gv 功能对应?

4

1 回答 1

0

尝试使用此代码 - 基于e.Item.FindControl

 if (e.CommandName.Equals("Insert"))
    {
        TextBox txtNewName = (TextBox)e.Item.FindControl("txtNewName");
        TextBox txtNewLame = (TextBox)e.Item.FindControl("txtNewLname");

....

    }
于 2012-09-13T19:51:32.883 回答