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

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...
LVL 10
GlobaLevelProgrammerAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
devlab2012Connect With a Mentor Commented:
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
 
GlobaLevelProgrammerAuthor Commented:
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
 
leakim971PluritechnicianCommented:
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
GlobaLevelProgrammerAuthor Commented:
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
 
devlab2012Commented:
Change the following line:
Session("SessionUserFirstName") = textVal

To:
HttpContext.Current.Session("SessionUserFirstName") = textVal
0
 
GlobaLevelProgrammerAuthor Commented:
DevLab...Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.