[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1823
  • Last Modified:

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

0
tristamane
Asked:
tristamane
1 Solution
 
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
 
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

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now