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:CollapsiblePanelExten der ID="collapsePostView" runat="server"
TargetControlID="panelPost ViewConten t"
ExpandControlID="panelPost ViewTitle"
CollapseControlID="panelPo stViewTitl e"
Collapsed="true"
ExpandDirection="Vertical"
TextLabelID="Label1"
ExpandedText="Hide Form"
CollapsedText="Reply"
CollapsedImage="~/App_Them es/images/ button-go. gif"
ExpandedImage="~/App_Theme s/images/b utton-go.g if"
/>
<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_AddRep ly"
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>
<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:CollapsiblePanelExten
TargetControlID="panelPost
ExpandControlID="panelPost
CollapseControlID="panelPo
Collapsed="true"
ExpandDirection="Vertical"
TextLabelID="Label1"
ExpandedText="Hide Form"
CollapsedText="Reply"
CollapsedImage="~/App_Them
ExpandedImage="~/App_Theme
/>
<asp:Panel ID="panelPostViewTitle" runat="server">
<asp:Label ID="Label1" runat="server">Reply</asp:
</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_AddRep
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");
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Place the PostID and row index in the CommandArgument.
<asp:LinkButton ID="btnReplySubmit" runat="server"
Text="Submit"
OnCommand="grdPosts_AddRep
CommandName="AddReply"
CommandArgument='<%# Eval("PostID") + "," + string.Format("{0}", ((GridViewRow)Container).R
/>
Split the argument and find the control.
// Split the command args
string[] commandArguments = e.CommandArgument.ToString
// Set the post, index and new id
int? postID = Convert.ToInt32(commandArg
int index = Convert.ToInt32(commandArg
// Find control
TextBox txtPostReply = (TextBox)grdPosts.Rows[ind