ASP.NET GridView RowCommand 文本框为空

发布于 2024-12-03 01:50:40 字数 1552 浏览 0 评论 0原文

您好,我有一个网格视图,每行都有一个文本框,我试图获取 RowCommand 事件中的值。下面的代码适用于除第一行之外的所有行。第一行的 textbox.text 值始终为空。

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_OnRowCommand" >
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                 Title <%#  Eval("Title")%>

                 <asp:TextBox ID="TextBoxAddPost" runat="server"></asp:TextBox>

                <asp:LinkButton ID="LinkButtonAddPost" CommandName="AddPost" CommandArgument='<%# Eval("postId") %>' runat="server">Add Post</asp:LinkButton>

            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
    if(IsPostBack)
        bindGridView();
}    

protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
        if (e.CommandName == "AddPost")
        {
                GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

                TextBox textBox = (TextBox)row.FindControl("TextBoxAddPost");

                //Empty for first row but works for all others

                Debug.WriteLine("row: " + row.RowIndex +  ", textBox:" + textBox.Text.Trim());

                 GridView1.DataBind();
        }
}

出于说明目的,上述代码已被简化。每行实际上包含一个子网格视图,因此为什么需要每行中的文本框。我担心 page_load 中的绑定会覆盖文本框的值,但是,如果没有 page_load 绑定,则不会触发 rowCommand 事件。

我觉得有点奇怪,它对除第一行之外的所有行都适用。

Hi I have a grid view with a textbox in each row that im trying to get the value of in the RowCommand event. The below code works fine for all rows expect the first one. The textbox.text value for the first row in always empty.

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_OnRowCommand" >
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                 Title <%#  Eval("Title")%>

                 <asp:TextBox ID="TextBoxAddPost" runat="server"></asp:TextBox>

                <asp:LinkButton ID="LinkButtonAddPost" CommandName="AddPost" CommandArgument='<%# Eval("postId") %>' runat="server">Add Post</asp:LinkButton>

            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    if(IsPostBack)
        bindGridView();
}    

protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
        if (e.CommandName == "AddPost")
        {
                GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

                TextBox textBox = (TextBox)row.FindControl("TextBoxAddPost");

                //Empty for first row but works for all others

                Debug.WriteLine("row: " + row.RowIndex +  ", textBox:" + textBox.Text.Trim());

                 GridView1.DataBind();
        }
}

The above code has been simplified for illustration purposes. Each row actually contains a child gridview, hence why in need the text box in each row. I fear that the binding in the page_load is overwriting the value of the text box, however, without the page_load binding, the rowCommand Event is not fired.

I find i a bit strange the it works fine for all rows except the first.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

屌丝范 2024-12-10 01:50:40

要从文本框中获取数据,您必须首先通过放置以下代码来设置文本属性。

<asp:TextBox ID="TextBoxAddPost" runat="server" Text='<%# Eval("Title") %>'></asp:TextBox>

它肯定会从文本框中给出值。

无论哪种方式,您还可以设置 gridview 的 datakeynames 属性。单击此处获取数据键名称

For getting data from textbox, You have to set text property first by putting below code.

<asp:TextBox ID="TextBoxAddPost" runat="server" Text='<%# Eval("Title") %>'></asp:TextBox>

It will definitely give value from textbox.

Either way, You can also set datakeynames property of gridview.Click here for datakeynames

著墨染雨君画夕 2024-12-10 01:50:40

我尝试了这个,效果很好,GridView1_OnRowCommand 通过单击 LinkBut​​tonAddPost: 触发

 <asp:GridView ID="GridView1"  runat="server"  OnRowCommand="GridView1_RowCommand">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:TextBox ID="TextBoxAddPost" runat="server" Text='<%# Eval("ID") %>'></asp:TextBox>
                        <asp:LinkButton ID="LinkButtonAddPost" CommandName="AddPost" CommandArgument='<%# Eval("ID") %>' runat="server">Add Post</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

,并像这样更改您的 page_load 事件:

 protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = Data.RequestPaymentDB.GetRequestPaymentByRequestID(9208060001);
        GridView1.DataBind();
    }

将您的代码与我的代码进行比较。

I tried this and it works fine,GridView1_OnRowCommand fires by clicking on LinkButtonAddPost:

 <asp:GridView ID="GridView1"  runat="server"  OnRowCommand="GridView1_RowCommand">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:TextBox ID="TextBoxAddPost" runat="server" Text='<%# Eval("ID") %>'></asp:TextBox>
                        <asp:LinkButton ID="LinkButtonAddPost" CommandName="AddPost" CommandArgument='<%# Eval("ID") %>' runat="server">Add Post</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

and change your page_load event like this:

 protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = Data.RequestPaymentDB.GetRequestPaymentByRequestID(9208060001);
        GridView1.DataBind();
    }

compare your code with mine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文