Solved

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

Posted on 2011-02-15
6
301 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
  • 3
  • 2
6 Comments
 
LVL 10

Author Comment

by:GlobaLevel
Comment Utility
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
Comment Utility
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
Comment Utility
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 10

Author Comment

by:GlobaLevel
Comment Utility
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
Comment Utility
Change the following line:
Session("SessionUserFirstName") = textVal

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

Author Comment

by:GlobaLevel
Comment Utility
DevLab...Thank you!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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 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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now