Frage

I am passing values generated in C# to my Javascript function like -

C#

 protected int Month;
 protected int Day;
 protected int Minute;
 protected int Hour;

 // set these later in code

Aspx

function test() {

                  Month: '<%= Month%>'
                  Day: '<%= Day%>'
                  Minute: '<%= Minute%>'
                  Hour: '<%= Hour%>'

However as the page reloads when various controls are hit, these variables are being reset to empty. Is there a way I can keep these values so that they are not lost on reload?

War es hilfreich?

Lösung

You may just define all your fields as properties with values stored in the ViewState, like the example below for Month:

protected int Month
{
    get { return this.ViewState["Month"] != null ? (int)this.ViewState["Month"] : 0; }
    set { this.ViewState["Month"] = value; }
}

Andere Tipps

You can add the values to a hidden field or some other control on the page that will maintain the value through a page load (through viewstate)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top