HEEN
HEEN

Reputation: 4721

Initializing global session variable on selection of dropdown value

I have a page where there is a dropdownlist.

Now what I want is, whenever I select the value from dropdownlist. A global session should get initialized. how to do that in asp.net.

Here is my code

<td align="right" width="8%">
  <span class="xy8">Warehouse :</span>
</td>
<td align="center">
  <asp:DropDownList ID="ddlPreferencesWh" runat="server" Width="150"> </asp:DropDownList>
</td>

How to do that in asp.net. whenever I select any value from the dropdownlist

UPDATED

Code behind;-

protected void Bindwarehouse()
{
    DataTable dtregion = CF.ExecuteDT("select Type_Desc, Master_Mkey from type_mst_a where type_code='WH'");
    ddlPreferencesWh.DataTextField = "Type_Desc";
    ddlPreferencesWh.DataValueField = "Master_Mkey";
    ddlPreferencesWh.DataSource = dtregion;
    ddlPreferencesWh.DataBind();
    ddlPreferencesWh.Items.Insert(0, new ListItem("---Select---", "0"));
}

Upvotes: 1

Views: 923

Answers (1)

Kasun Koswattha
Kasun Koswattha

Reputation: 2461

Your dropdownlist has a method called "selectedIndexChanged". When even someone changes the drodown list's selected index, this method will fired in the server side.

To call the server side method in your dropdownlist please use the below code.

<asp:DropDownList ID="ddl" runat="server" OnSelectedIndexChanged="YourMethodinServerSide">

sever side method -

protected void YourMethodinServerSide(object sender, EventArgs e)
{
}

in this "YourMethodinServerSide" method you can write your code to initiate the session by creating a session variable.

Session["variable_name"]=ddl.selectedindex.value;
Response.Redirect("Home.aspx");

So whenever you change the dropdownlist value, Session["variable_name"] will get updated.

If you have a base class for managing the sessions, then you can pass the dropdownlist value to that class.

Upvotes: 2

Related Questions