Find button control in GridView within an UpdatePanel

I am trying to grab the value of the txtPostRely textbox control but I get a null exception. Here is the code.

<asp:UpdatePanel ID="panelPost" runat="server">
        <ContentTemplate>
            <asp:GridView ID="grdPosts" runat="server"
                AutoGenerateColumns="False"
                Width="100%"
                AllowPaging="True"
                PageSize="5"
                DataSourceID="sqldsPosts"
                DataKeyNames="PostID">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <h2><%# Eval("Title") %></h2>
                           
                            <cc1:CollapsiblePanelExtender ID="collapsePostView" runat="server"
                                TargetControlID="panelPostViewContent"
                                ExpandControlID="panelPostViewTitle"
                                CollapseControlID="panelPostViewTitle"
                                Collapsed="true"
                                ExpandDirection="Vertical"
                                TextLabelID="Label1"
                                ExpandedText="Hide Form"
                                CollapsedText="Reply"
                                CollapsedImage="~/App_Themes/images/button-go.gif"
                                ExpandedImage="~/App_Themes/images/button-go.gif"
                                />
                           
                            <asp:Panel ID="panelPostViewTitle" runat="server">
                                <asp:Label ID="Label1" runat="server">Reply</asp:Label>
                            </asp:Panel>
                            <asp:Panel ID="panelPostViewContent" runat="server">
                                <%# Eval("Message") %>
                               
                                Reply:
                                <br /><asp:TextBox ID="txtPostReply" runat="server" Rows="5" Columns="40"></asp:TextBox>
                                <br /><asp:LinkButton ID="btnReplySubmit" runat="server"
                                    Text="Submit"
                                    OnCommand="grdPosts_AddReply"
                                    CommandName="AddReply"
                                    CommandArgument='<%# Eval("PostID") %>'
                                    />
                            </asp:Panel>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
           
            <asp:SqlDataSource ID="sqldsPosts" runat="server"
                ConnectionString="<%$ ConnectionStrings:SQL %>"
                SelectCommand="SELECT * FROM [ForumPosts] ORDER BY [PostID] DESC">
            </asp:SqlDataSource>
        </ContentTemplate>
    </asp:UpdatePanel>
protected void grdPosts_AddReply(object sender, System.Web.UI.WebControls.CommandEventArgs e)
{
	int? postID = Convert.ToInt32(e.CommandArgument.ToString());
 
	// Find control
	TextBox txtPostReply = (TextBox)panelPostViewContent.FindControl("txtPostReply");
}

Open in new window

tristamaneAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

techExtremeCommented:
Hi,
You cannot directly get access to the textbox.
Here's the steps:

1. Get the currentRowIndex
2. Using that, do findcontrol on the current row and fetch 'panelPostViewContent' control which is of type panel.
3. Do FindControl again on the previously found panel control , but now for 'txtPostReply'
4. If you get the textbox in step 3, i.e. it is not null (c#.net) or nothing (vb.net), then you are good to go.

If this doesn't work for you, let me know.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tristamaneAuthor Commented:
Here is my fix.

Place the PostID and row index in the CommandArgument.

<asp:LinkButton ID="btnReplySubmit" runat="server"
                                Text="Submit"
                                OnCommand="grdPosts_AddReply"
                                CommandName="AddReply"
                                CommandArgument='<%# Eval("PostID") + "," + string.Format("{0}", ((GridViewRow)Container).RowIndex + 1) %>'
                                />

Split the argument and find the control.

// Split the command args
string[] commandArguments = e.CommandArgument.ToString().Split(',');

// Set the post, index and new id
int? postID = Convert.ToInt32(commandArguments[0]);
int index = Convert.ToInt32(commandArguments[1]);

// Find control
TextBox txtPostReply = (TextBox)grdPosts.Rows[index - 1].Cells[0].FindControl("txtPostReply");
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.