Solved

asp.net - javascript - pass a value from a textbox on client into sessionvaraible...

Posted on 2011-02-15
6
307 Views
Last Modified: 2012-05-11
on click of a link I need to pass a value from a textbox into a session varaible that can be used on the server...
0
Comment
Question by:GlobaLevel
[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
  • 3
  • 2
6 Comments
 
LVL 10

Author Comment

by:GlobaLevel
ID: 34898515
something like this...

------

<a href="http://localhost:1757/test/wait_new_1.aspx" target="_blank" onclick="return popCC(this);">send values</a>

<script language="JavaScript">
         
function popCC(url) {
  sessionvaraible = textbox1.text

}
</script>

0
 
LVL 13

Accepted Solution

by:
devlab2012 earned 500 total points
ID: 34898829
Use LinkButton instead of a normal anchor tag and use its server side click event.

Or You have to submit the value to the server using AJAX. Here is the complete details.

Create a web method on the page, that will accept value and set the session variable on the server:

(You need to add "using System.Web.Services" at the top of the page).
Here is the server side code:
[WebMethod()]
public static void SetVariable(String value)
{
    Session[name_of_session_variable] = value;
}


Now add a script manager with EnablePageMethods set to true.

Now, add link as:

<a href="#" onclick="setSession();">Link text</a>

add javascript code as:

<script>

function setSession()
{
    var textVal = document.getElementById("<%=TextBox1.ClientID%>").value;
    PageMethods.SetVariable(textVal, setVariableSuccess);
}

function setVariableSuccess()
{
  //do nothing here
}

</script>
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34900023
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 10

Author Comment

by:GlobaLevel
ID: 34900401
devlab...

with this...
    <WebMethod()> _
    Public Shared Sub SetVariable(ByVal textVal As String)
        Session("SessionUserFirstName") = textVal
    End Sub

I get this...
Cannot refer to an instance member of a class from within a shared method or shared member intializer without an explicit instance of the class
0
 
LVL 13

Expert Comment

by:devlab2012
ID: 34904616
Change the following line:
Session("SessionUserFirstName") = textVal

To:
HttpContext.Current.Session("SessionUserFirstName") = textVal
0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 34910666
DevLab...Thank you!
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

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'…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

733 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