Link to home
Start Free TrialLog in
Avatar of tristamane
tristamane

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of techExtreme
techExtreme
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tristamane
tristamane

ASKER

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");