Pergunta

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?

Foi útil?

Solução

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

Outras dicas

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)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top