Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I move a JavaScript Variable to an asp.net code behind Variable (C#)

Posted on 2009-04-05
3
Medium Priority
?
382 Views
Last Modified: 2012-05-06
I have a java function that returns a value, in a var named Rm  I want to move that value to a ASP.NET variable named netRm.  Can someone show me how to do this.  Im using C#  in my code behind page.

Below is my code:


Aspx:
 
<label for="x1">x1:</label>
<script> var Rm = x1;</script>
<asp:Label ID=" Lblx1" runat="server" Text="Label"></asp:Label>
 
 
CodeBH:
 
String netRm;
Lblx1.text = netRm;

Open in new window

0
Comment
Question by:bmanmike39
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
sm394 earned 2000 total points
ID: 24072885
one alternative could be the use of hidden field
i.e

ASPX
 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestPro.WebForm1" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
 
    <script type="text/javascript">
        function Test() {
 
            var i = "aaa";
            document.getElementById('<%=hdnField.ClientID%>').value = i;
        }
    </script>
 
</head>
<body >
    <form id="form1" runat="server">
    <div>
   
        <asp:HiddenField runat="server" ID="hdnField" />
        <asp:Button runat="server" ID="btnTest" Text="GetVal" onclick="btnTest_Click"  OnClientClick="Test();" />
    </div>
    </form>
</body>
</html>
 
 
 
Code Behind
 
   protected void btnTest_Click(object sender, EventArgs e)
        {
            Response.Write(hdnField.Value);
        }

Open in new window

0
 
LVL 13

Expert Comment

by:sm394
ID: 24072934
once you have value in hidden field you can assign to your variable

public string Name;

        protected void btnTest_Click(object sender, EventArgs e)
        {
            Response.Write(hdnField.Value);
            Name = hdnField.Value;
        }

or in aspx

  <%=Name %>
0
 

Author Closing Comment

by:bmanmike39
ID: 31566807
Thanks!!!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question