5

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.

abatishchev
  • 92,232
  • 78
  • 284
  • 421
user346443
  • 4,338
  • 13
  • 53
  • 75
  • 5
    Do you really want to bind the gridview ONLY if it is a postback, it is usual to do it the other way round. – Ben Robinson Sep 05 '11 at 17:01
  • Well i initially bind the gridview through a search function, but if the gridview is not bound on the post back the rowCommand event is not fired. – user346443 Sep 05 '11 at 17:08
  • Ok, so i set the MasterPages ContentPlaceHolder EnableViewState to false, and it worked. – user346443 Sep 05 '11 at 17:23
  • 1
    @user: if it is working now i would suggest to add your solution as answer and accept it later or (because it's very specific and not very helpful for other) simply delete it. – Tim Schmelter Sep 05 '11 at 17:48
  • doing a databind inside a rowcommand seems kind of odd. I've always found that I can use e.CommandArgs to pass into the rowcommand what I need. That is something like put into your markup similar to what you already have for CommandArgument='' and do the same for your textbox. Maybe make not sure this is a help, but I have found that using findcontrol in a template is always problematic. – Peter Kellner Dec 31 '11 at 20:42
  • your question is not clear, bindGridView() method contains GridView1.datasource and GridView1.databind or more? how you fill gridview1 for first time? – Masoumeh Karvar Nov 09 '13 at 11:25
  • `DataKeyNames` attribute in `GridView` definition would greatly enhance your life. – Alex Kudryashev Jul 21 '16 at 03:36

2 Answers2

0

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

Hiral
  • 465
  • 3
  • 12
0

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.

Masoumeh Karvar
  • 705
  • 1
  • 12
  • 27