SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePοint Troll disgracian's Avatar
    Join Date
    Aug 2006
    Location
    Samsara
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Hardships with ASP.NET page lifecycle

    Hi all,

    I'm trying to write what looks like a pretty simple user control for selecting a range of dates.

    I'm using two CalendarExtender AJAX ToolKit things, which are each linked to a TextBox control. There are other buttons to shift the dates around one month or week at a time, and they work fine using session variables to retain selected date values across postbacks. It's the selecting arbitrary dates with the CalendarExtender that is failing.

    I'm using the OnClientDateSelectionChanged attribute to run a Javascript function that writes the selected value to a hidden field, then writes that to a session variable:
    Code:
    function updateTo()
    {
        var dateStr = document.getElementById( '<%=txtTo.ClientID %>' ).value;
        document.getElementById( '<%=hiddenTo.ClientID %>' ).value = dateStr;
        <%
            Session["userDateTo"] = hiddenTo.Value;
        %>
    }
    The problem is that it's always one page reload behind. The first time the Update button is pressed, the new date values are replaced in the textbox with the old values. The second time it's pressed, the old values are finally replaced with the new values.

    By stepping through the code with breakpoints, I've found that on the first postback the session variables created by the Javascript exist (in that the keys exist) but the values are an empty string. On the second postback they then get their values. I really don't understand how/why this is happening the way it is.

    I can provide necessary code snippets if required.

    Cheers,
    D.

  2. #2
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The bad news:

    This is not a page lifecycle problem. I am afraid you misunderstand how AJAX works. The updateTo function executes in the browser and has no access to the Session object.

    The good news:

    This isn't hard at all, and I believe you are complicating matters. Look at this sample:

    DateRange.ascx:

    Code ASP:
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="DateRange.ascx.cs" Inherits="DateRange" %>
    From: <asp:TextBox ID="txtFrom" runat="server" />
    To: <asp:TextBox ID="txtTo" runat="server" />
    <ajax:CalendarExtender ID="calFrom" TargetControlID="txtFrom" runat="server" Animated="true" />
    <ajax:CalendarExtender ID="calTo" TargetControlID="txtTo" runat="server" />

    DateRange.ascx.cs:

    Code Csharp:
    using System;
     
    public partial class DateRange : System.Web.UI.UserControl
    {
        public string Format {
            set{ 
                calFrom.Format = value; 
                calTo.Format = value; 
            }
            get{ return calFrom.Format; }
        }
     
        public DateTime? From
        {
            get { return calFrom.SelectedDate; }
            set { calFrom.SelectedDate = value; }
        }
     
        public DateTime? To
        {
            get { return calTo.SelectedDate; }
            set { calTo.SelectedDate = value; }
        }
    }

    use in page:

    Code:
    <uc:DateRange ID="Range" Format="yyyy-MM-dd" runat="server" To="2010-12-12" />

  3. #3
    SitePοint Troll disgracian's Avatar
    Join Date
    Aug 2006
    Location
    Samsara
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The other good news is that, overly complicated though it may be, I did manage to get it working in my own roundabout way. In the end I was able to get the values out of the hidden field.

    Do you happen to have links to any good literature that might enlighten me as to how AJAX meshes together with ASP.NET? I'm trying to learn this all very quickly. All of my experience has been in the old CGI model (mostly in Perl) and hand-made Javascript. Right now I feel at the mercy of Microsoft's API because the inner workings are very much a magic-factory to me at the moment.

    Yes I've read a few things already, but my mileage tends to vary quite a lot from the examples (even those from MSDN).

    Cheers,
    D.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •