You are not logged in.

#1 04 Apr 2008 5:57 am

azzlack
Senior Member
From: Bergen, Norway
Registered: Jan 2008
Posts: 26
Website

Setting DateTime on click event

I'm trying to set a DateTime object to the current date and time when the user clicks a button.

My code looks like this:

Code:

public partial class _Default : System.Web.UI.Page
    {
        DateTime Arrival;

...

protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            if (Wizard1.ActiveStep.ID == "Step_Arrival")
            {
                Arrival = DateTime.Now;
            }
        }

...

protected void Wizard1_ActiveStepChanged(object sender, EventArgs e)
        {

            if (Wizard1.ActiveStep.ID == "Step_Report")
            {
                Label2.Text = "Ankomst tid: " + Arrival.ToString();
            }
        }

But the result is only this:

Ankomst tid: 01.01.0001 00:00:00

The wizard control resides inside an UpdatePanel. (Don't know if it is of any consequence).

I have tried using labels instead, and it works good.

Code:

lblArrival.Text = DateTime.Now.ToString();

and then

Label2.Text = "Ankomst tid: " + lblArrival.Text;

My reason for not wanting to use labels is that it sounds more like an ugly hack to make stuff work than proper programming.
As a student, I like to learn the proper ways of doing things, not just the workarounds.

Regards,
Ove Andersen

Last edited by azzlack (04 Apr 2008 6:03 am)


Web Developer and Designer.
Currently studying for Bachelor of Computer Science degree ...

Offline

 

#2 04 Apr 2008 7:06 am

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Setting DateTime on click event

well the reason it did what it did using a class variable is due to that crappy asp.net page lifecycle.  the class is instantiated when the page loads.  things are assigned, then the class is used to generate the html sent to the client, then the class is destroyed (on every request).  setting a class member won't do much as you could see.

maintaining info between page requests is typically done w/ the session. the reason it works if you assign it to a label on the page is that the label becomes a form element, and when the form is posted back to the server, the label persists the time.  the best route would be to have a fake property on the class that wraps the session hashtable:

Code:

public class MyPage : Page {

    private DateTime Arrival {
        get {
            if(Session["Arrival"] == null) {
                Session["Arrival"] = DateTime.Now;
            }
            return (DateTime)Session["Arrival"];
        }
        set {
            Session["Arrival] = value;
        }
    }

    protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e) {
        if (Wizard1.ActiveStep.ID == "Step_Arrival") {
            Arrival = DateTime.Now;
        }
    }

    protected void Wizard1_ActiveStepChanged(object sender, EventArgs e) {
        if (Wizard1.ActiveStep.ID == "Step_Report") {
            Label2.Text = "Ankomst tid: " + Arrival.ToString();
        }
    }
}

Offline

 



© 2003 - 2017 NullFX
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License