• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 454
  • Last Modified:

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.
0
barnesco
Asked:
barnesco
  • 5
  • 2
1 Solution
 
the_paabCommented:
On client side you can't work with object that are not rendered as html elements in displayed html page. You can go around this by using of nonvisible server element.

in javascript:
document.getElementById('label1').value = 1;              

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

in code behind:
  rbRecurEventValue.value = label1.Text
  If rbRecurEventValue = 1 Then
       [do something]
   End if
0
 
barnescoAuthor Commented:
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.
0
 
barnescoAuthor Commented:
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
barnescoAuthor Commented:
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.
0
 
barnescoAuthor Commented:
The question now is how do you assign a javascript value to an aspx control?
0
 
barnescoAuthor Commented:
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.

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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now