1
 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" 
    BorderStyle="None" BorderWidth="1px" CellPadding="4" 
    DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Horizontal" 
    Width="900px">
    <Columns>
        <asp:TemplateField HeaderText="Date" SortExpression="Date">
            <EditItemTemplate>
                <asp:TextBox ID="txtStartDate" runat="server" Text='<%# Bind("Date") %>'></asp:TextBox>
                <asp:CalendarExtender
                                ID="CalendarExtender1"
                                onprerender="AjaxCalendar_PreRender" 
                                runat="server"
                                TargetControlID="txtStartDate"
                                Format="dd/MM/yyyy"  />

            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                ErrorMessage="Date is required" Font-Italic="True" Font-Size="Small" 
                ForeColor="Red" ControlToValidate="txtStartDate"></asp:RequiredFieldValidator>


            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Date") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle HorizontalAlign="Left" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Time" SortExpression="Start_Time">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Start_Time") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Start_Time") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle HorizontalAlign="Left" />
        </asp:TemplateField>

        <asp:CommandField ShowEditButton="True">
        <ItemStyle ForeColor="#003366" />
        </asp:CommandField>
    </Columns>
</asp:GridView>

I want to set the calendar with certain range of date.

protected void AjaxCalendar_PreRender(object sender, EventArgs e)
{
    DateTime startDate = DateTime.Now.AddDays(1);
    DateTime endDate = DateTime.Now.AddDays(60);
    CalendarExtender ce = (CalendarExtender)GridView1.FindControl("CalendarExtender1");
    ce.StartDate = startDate;
    ce.EndDate = endDate;
}

This is what I do, but I got Object reference not set to an instance of an object. Is it the GridView FindControl caused the error?

John Saunders
  • 157,405
  • 24
  • 229
  • 388
user236501
  • 7,930
  • 23
  • 80
  • 116
  • Where exactly is the exception happening? Also, see http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net – John Saunders Jul 26 '12 at 18:35
  • CalendarExtender ce = (CalendarExtender)GridView1.FindControl("CalendarExtender1"); THis line – user236501 Jul 27 '12 at 01:43

1 Answers1

0

You need to move that logic to the rowcreated event: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcreated

Remember what you got on a gridview is a bunch of items, not a single control. Those items are grouped by templates, some of them are:

  • ItemTemplate
  • EditItemTemplate
  • InsertItemTemplate

And you do not have the extender for all the templates, since it is not necessary. That is why this will not work:

CalendarExtender ce = (CalendarExtender)GridView1.FindControl("CalendarExtender1");

Since the actual CalendarExtender1 does not exist inside the gridview, but inside of each item, that can have multiple templates.

Hope I am explaining myself and you can finish that.

zaiboot
  • 131
  • 5