Answered by:
Need help: Findcontrol Calendar in Edit Template in GridView

Question
-
User126014556 posted
Hi all,
I am trying to hide the Calendar control in a edit template in the GridView. I tried Findcontrol but I still get the following error message:
Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 20: foreach (GridViewRow gridViewRow in GridView2.Rows) Line 21: { Line 22: ((Calendar)gridViewRow.FindControl("Calendar1")).Visible = true; Line 23: } Line 24: }
My code page:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { foreach (GridViewRow gridViewRow in GridView2.Rows) { ((Calendar)gridViewRow.FindControl("Calendar1")).Visible = true; } } protected void Calendar1_SelectionChanged(object sender, EventArgs e) { foreach (GridViewRow gridViewRow in GridView2.Rows) { ((TextBox)gridViewRow.FindControl("TextBox5")).Text = ((Calendar)gridViewRow.FindControl("Calendar1")).SelectedDate.ToString(); ((Calendar)gridViewRow.FindControl("Calendar1")).Visible = false; } }
My html page
<asp:TemplateField HeaderText="Approval" SortExpression="DateApproved"> <EditItemTemplate> <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("DateApproved", "{0:d}") %>' Width="80px"></asp:TextBox> <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/Calendar.png" onclick="ImageButton1_Click" /> <asp:Calendar ID="Calendar1" runat="server" onselectionchanged="Calendar1_SelectionChanged" Visible="False"></asp:Calendar> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("DateApproved", "{0:d}") %>'></asp:Label> </ItemTemplate> <HeaderStyle HorizontalAlign="Left" VerticalAlign="Top" Wrap="False" /> <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" Wrap="True" /> </asp:TemplateField> <asp:BoundField DataField="DateModified" HeaderText="Modified" SortExpression="DateModified" DataFormatString="{0:d}" ReadOnly="True"> <HeaderStyle HorizontalAlign="Left" VerticalAlign="Top" Width="100px" Wrap="False" /> <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" Wrap="True" /> </asp:BoundField>
I appreciate any help on this issue.
Thanks in advance.
Thursday, May 19, 2016 10:41 AM
Answers
-
User61956409 posted
Hi Matt99,
According to your code, it seems that you’d like to find and show Calendar control from GridViewRow that is edited, if that is the case, you could try to use the following code.
protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { GridViewRow gridViewRow = (GridViewRow)((ImageButton)sender).NamingContainer; ((Calendar)gridViewRow.FindControl("Calendar1")).Visible = true; }
Best Regards,
Fei Han
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 20, 2016 2:15 AM
All replies
-
User61956409 posted
Hi Matt99,
According to your code, it seems that you’d like to find and show Calendar control from GridViewRow that is edited, if that is the case, you could try to use the following code.
protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { GridViewRow gridViewRow = (GridViewRow)((ImageButton)sender).NamingContainer; ((Calendar)gridViewRow.FindControl("Calendar1")).Visible = true; }
Best Regards,
Fei Han
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 20, 2016 2:15 AM -
User126014556 posted
Thank you,
Saturday, May 21, 2016 10:26 AM