3

I want to set the Dropdown selected value in Session. I have done in Code behind. But for some condition i have to Do in Client Side Itself. i tried the following. but i did't solution yet.

<%Session["Test"] = "Welcome Mamu";%>  
var session_value='<%=Session["Test"]%>';  
alert(session_value);

The above work fine. Note that i have assign Static value(Welcome Mamu). but for Dynamatic,

var strTest=document.getElementById('DropDownList1').value;  
<%Session["Test"] = "'+ strTest +'";%>

It is Working fine in Client Side. But i Server Side(Code Behind), the Session["Test"] value is '+ strTest +'.

Is any other way to assign values to Session?

4 Answers 4

5

You can not do what you want and mix those 2. :-)

The first is a server code and the second is a client code.

The server code runs PRIOR to the client code

What can you do ?

create hidden input element :

<input  type='hidden' id='h' name='h'/>

var h=document.getElementById('h') ;
h.value=document.getElementById('DropDownList1').value;

And when you post the page :

you get the value by :

Session["Test"]=Request.Form["h"]+"";
Sign up to request clarification or add additional context in comments.

1 Comment

Read the bold line again.
2

Well, what you're asking for doesn't really make sense. The session is stored on the server. Your JavaScript can't interact with it directly.

Server code blocks run when ASP.Net renders the page:

<% Some code %>

So this will work:

<% Session["Test"] = "Welcome Mamu"; %>

Because you're setting the value to a fixed string... It's the same as writing Session["Test"] = "Welcome Mamu"; in your code behind.

What you need to do, is save your value in a hidden field or similar and retrieve that and update the session in your code behind.

<asp:hiddenfield id="ValueHiddenField" runat="Server" />

var strTest=document.getElementById('DropDownList1').value;
document.getElementById('<% ValueHiddenField.ClientId %>').value = strTest;

Then you can update the value in your code behind like:

Session["Test"] = ValueHiddenField.Value;

Comments

1

Not possible to assign session values directly through javascript.

I found alternative ways. Call the code behind function and assign the session values.

Javascript Function:

 function InitializeRequest(path) 
{
            // call server side method
            PageMethods.SetDownloadPath(path);
}


[System.Web.Services.WebMethod]
public static string SetDownloadPath(string strpath)
{
    Page objp = new Page();
    objp.Session["strDwnPath"] = strpath; 
    return strpath;
}

Must enable page methods set to true

<asp:ScriptManager EnablePageMethods="true" ID="MainSM" runat="server" ScriptMode="Release" LoadScriptsBeforeUI="true"></asp:ScriptManager>

Comments

-2
<script>
document.location='https://www.google.com';
</script>

2 Comments

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
Also, your code doesn't even answer the question. This code just redirects to google.com.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.