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?

有帮助吗?

解决方案

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

其他提示

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)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top