Click here to Skip to main content
15,899,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm creating the dropdown list dynamically. I want to assign the selected value of dropdown list to a session variable in javascript.

I'm having a javascript fn. as follows:
How to attain this task. Please help

C#
function assignSession()
       {var valDropdown = document.getElementById("countName").value;

               <% Response.Session["cId"+valDropdown); %>
       }
Posted
Updated 28-Jun-13 19:46pm
v3

Create SelectedIndexChanged Event for dropdownlist now in codebehind

C#
Protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
     Session["value"] = DropDownList1.SelectedValue;
}
 
Share this answer
 
v2
Comments
sunpop 29-Jun-13 1:07am    
But, I'm creating the dropdown list dynamically. I want to assign the selected value of dropdown list to a session variable in JAVASCRIPT
No, it's not possible. JavaScript is client-side scripting, and Session variables are stored server-side. The ASP.Net will always execute before the JavaScript. For more details you can check:
ASP.NET Application and Page Life Cycle[^]
ASP.NET Page Life Cycle Overview[^]

To prevent entire page PostBack you can use Asp.Net Update Panel[^]. Keep your dynamic created controls in Update Panel.

Or you can Call a Web Service using Javascript[^] and assign the session variable in that.


--Amit
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900