This is a migrated thread and some comments may be shown as answers.

Viewstate and DatePicker OnSelectedDateChanged event

1 Answer 264 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 2
Yeroon asked on 30 Oct 2012, 11:42 AM

I was wondering why the following is happening. I have a page with a DatePicker. I set the OnSelectedDateChanged event. All works fine. Autopostback is also set. A grid then gets rebound (calling grid.rebind in datechange event) to reflect new date range selection. Now I have a button on the form, which then triggers an action based on the selected items in the grid. All works fine when viewstate is enabled for both datepicker and grid.

But when I set the EnableViewState to false for the datepicker, all goes wrong. Because the viewstate on the datepicker is turned off, the OnSelectedDateChanged is not just fired when the date changes, but also when I click the button, or trigger any other server side event. This cause the grid to be rebound and my SelectedItems count is then zero.

Is this normal behavior and my understanding of viewstate is too limited? Since I dont need viewstate on datepicker just to rebind grid to correct date, I would prefer to leave viewstate off.

Example page to see the OnSelectedDateChanged event getting fired when viewstate is disabled on the datepicker and you trigger another event: (just enable viewstate on datepicker to see that event doesnt get triggered then if button is clicked)


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<head runat="server">
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server" EnableScriptCombine="true"
        OutputCompression="Forced" ScriptMode="Release">
            <%--Needed for JavaScript IntelliSense in VS2010--%>
            <%--For VS2008 replace RadScriptManager with ScriptManager--%>
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
        <asp:Button runat="server" ID="btnSelect" Text="Select" OnClick="btnSelect_Click" />
        <telerik:RadDatePicker ID="dp4" runat="server" MinDate="2006-01-01" AutoPostBack="True" OnSelectedDateChanged="dp4_Changed"
            Width="100px" EnableViewState="False">
            <Calendar ID="cal1" runat="server" />
            <DateInput ID="dip1" runat="server">


using System;
using Telerik.Web.UI.Calendar;
public partial class Test : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
    protected void btnSelect_Click(object sender, EventArgs e)
    protected void dp4_Changed(object sender, SelectedDateChangedEventArgs e)
        //set breakpoint
        string x = "";

1 Answer, 1 is accepted

Sort by
Telerik team
answered on 02 Nov 2012, 08:30 AM
Hello Jeroen,

Please note that this issue is not Telerik specific, rather a default behavior for the asp controls. When you disable the input's viewstate, it could not preserve the entered value, therefore, it needs to explicitly set it up which causes TextChanged event to fire on each postback to the server:
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged" EnableViewState="false"></asp:TextBox>

I hope the clarification was helpful.

All the best,
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Asked by
Top achievements
Rank 2
Answers by
Telerik team
Share this question