Link to home
Start Free TrialLog in
Avatar of barnesco
barnesco

asked on

Passing a javascript value to a code behind page

How would you pass a javascript value back to a code behind page in asp.net, such as:

FROM: (onclick function...)
   rbRecurEventValue.value = 1

TO CODE BEHIND:
   
   Public rbRecurEventValue as Integer

   If rbRecurEventValue = 1 Then
       [do something]
   End if

I'm receiving a value of 0 when passing it to the code behind page.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of the_paab
the_paab

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of barnesco
barnesco

ASKER

The javascript part makes sense, but label1 isn't an aspx control--it's an html element. How would the above code work then? Thanks.
I see what you're doing now--passing it to a hidden field, then passing it to the code behind variable, but I might be missing something. I have:

in javascript:
document.getElementById("lblRecurEvent").value=1;

in aspx:
<asp:Label ID="lblRecurEvent" runat="server" style="display:none;"></asp:Label>

in code behind:

RecurEvent = lblRecurEvent.Text
   If RecurEvent = 1 Then
       [do something]
   End if

Where is going wrong? Thanks.
It seems the value is not being set with document.getElementById("lblRecurEvent").value=1.

I've tried document.getElementById("lblRecurEvent").text=1, but that doesn't work, either.
The question now is how do you assign a javascript value to an aspx control?
Finally figured it out on two fronts:

First have to declare the variable, then assign the value as in:

    var elemRecurValue = document.getElementById("varname")
    elemRecurValue.value=1

Second, and this is the one that took all %$^&* day to figure out: You can't assign a value to a label using javascript!!! For some dumb reason, it just won't work. It will for a textbox, so I used a textbox hidden field, and yes, it works.

   
Sorry about the Label you are right it must be TextBox.