---
title: "How to set a asp:DropDownList SelectedValue to a Session Variable?"  
description: "How to set a asp:DropDownList SelectedValue to a Session Variable?"  
author: "Tom Cruser"  
published: 2014-12-21  
updated: 2014-12-22  
canonical: https://www.mindstick.com/forum/12804/how-to-set-a-asp-dropdownlist-selectedvalue-to-a-session-variable  
category: "asp.net"  
tags: ["c#", "session variable", "dropdown"]  
reading_time: 2 minutes  

---

# How to set a asp:DropDownList SelectedValue to a Session Variable?

There are several [articles](https://www.mindstick.com/articles/12872/how-to-write-articles-of-50-and-more-a-day-quick-and-easy) describing how to do this is [code behind](https://www.mindstick.com/forum/2199/call-javascript-s-function-from-code-behind) however:

Is it possible to set the value of a [dropdownlist](https://www.mindstick.com/articles/324837/how-to-bind-dropdownlist-in-mvc-core-from-database-using-entity-framework) to a [session variable](https://www.mindstick.com/forum/43/session-variable-in-java-script) on the [aspx page](https://www.mindstick.com/forum/218/how-do-i-create-an-aspx-page-that-periodically-refreshes-itself)?

I am using SqlDataSource to populate the dropdownlist so do not wish to add code behind if it can be avoided.

```
<asp:DropDownList ID="ddl" runat="Server" DataSourceID="sqlDS" DataValueField="ID" DataTextField="TEXT" AppendDataBoundItems="true">  <asp:ListItem Text="" Value="" Selected="True" /></asp:DropDownList> <asp:SqlDataSource ID="sqlDS" runat="Server" SelectCommand="spDS" SelectCommandType="StoredProcedure" />
```

[Set Session](https://www.mindstick.com/forum/2413/how-to-set-session-avassetexportsession-in-ios)("ID") as [selected value](https://www.mindstick.com/forum/525/get-selected-value-of-datagridviewcomboboxcolumn) on load?

The [dropdown list](https://www.mindstick.com/forum/760/dropdown-list-open-hide-behind-another-dropdown-list-on-ie-7) is already populated from the sqldatasource. I just want to set the initial value on [page load](https://www.mindstick.com/articles/12909/how-to-open-first-time-popup-on-page-load-in-website).

## Replies

### Reply by Anonymous User

you'll need an event for your dropdown list on change. Are you using C# or VB.net for your codebehind?

add to onSelectedIndexChanged="ddl_OnSelectedIndexChanged"

to your code behind add:

{this is C# vb is similar}

```
protected void ddl_OnSelectedIndexChanged(Object sender, EventArgs e){    Session["selectedID"] = ddl.selectedValue;}
```

to your page load, add

```
if (isPostback){    ddl.selectedValue = Session["selectedID"];}
```

### Reply by Anonymous User

You need a server side code in order to use Session. The following code doesn't requires code behind file, but again the code inside script will run at server side.

```
<asp:DropDownList ID="ddl" runat="Server"    DataSourceID="sqlDS"    DataValueField="ID"    DataTextField="TEXT"    AppendDataBoundItems="true"   OnSelectedIndexChanged="ddl_SelectedIndexChanged"    AutoPostBack="True">  <asp:ListItem Text="" Value="" Selected="True" /></asp:DropDownList> <asp:SqlDataSource ID="sqlDS" runat="Server"   SelectCommand="spDS" SelectCommandType="StoredProcedure" /> <script runat="server">    protected void ddl_SelectedIndexChanged(object sender, EventArgs e)    {        Session["SelecteValue"] = ddl.SelectedValue;    }</script>
```

Note: Make sure AutoPostBack="True" for DropDownList.


---

Original Source: https://www.mindstick.com/forum/12804/how-to-set-a-asp-dropdownlist-selectedvalue-to-a-session-variable

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
