Domanda

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?

È stato utile?

Soluzione

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

Altri suggerimenti

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)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top