0

Web アプリに問題があります。SQLデータテーブルに接続されたグリッドビューがあり、フッターにはテキストボックス(またはドロップダウンリスト)と「挿入」ボタンがあります。ボタンをクリックして、SQL テーブルに新しい行を挿入したいときに問題が発生します。テキストボックスからテキストを読み取りません (いくつかの文字列を入れる必要があります)。もちろん、一部のデータは null であってはならないため、挿入は行われません。行の編集にも同じ問題があります。

これは私のコードです:

グリッドビュー:

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

編集:まあ、それは今動作します。同様の問題が見つかり、著者は、Gridview に EnableViewState="false" を追加することで問題を解決できたと述べています。

私は試してみましたが、うまくいきました。:)

なぜこれが機能するのか、ここで答えられる人はいますか? そして、これは他の 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 に答える