Viewstate and DatePicker OnSelectedDateChanged event

2 posts, 0 answers
  1. Yeroon
    Yeroon avatar
    87 posts
    Member since:
    Oct 2012

    Posted 30 Oct 2012 Link to this post

    Hello,

    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)

    ASPX:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server" EnableScriptCombine="true"
            OutputCompression="Forced" ScriptMode="Release">
            <Scripts>
                <%--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" />
            </Scripts>
        </telerik:RadScriptManager>
        <div>
            <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">
                </DateInput>
            </telerik:RadDatePicker>
             
        </div>
        </form>
    </body>
    </html>


    Code-behind:

    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 = "";
     
        }
    }




  2. Eyup
    Admin
    Eyup avatar
    3007 posts

    Posted 02 Nov 2012 Link to this post

    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,
    Eyup
    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top