I'm trying to refresh page when an item of the drop down list is selected.
DropDownList never gets last selected item, it always restores the one which is saved in ViewState. ViewState doesn't want to remember new stuff, it always keeps one and the same info.
DEFAULT ASPX PAGE
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ExperimentalPage.aspx.cs"
Inherits="ExperimentalPage" Title="Enperimental Page" EnableViewState="True" %>
<asp:Content ID="ExperimentalLeftPane" ContentPlaceHolderID="cph_LeftPane" Runat="Server">
<div style="position: absolute; left: 0px; top: 35px; width: 250px;">
<asp:DropDownList ID="dd1" runat="server" OnSelectedIndexChanged="RefreshPage" AutoPostBack="True" CssClass="class_DropDown" Style="width: 250px; margin-bottom: 3px;"/>
</div>
</asp:Content>
DEFAULT ASPX.CS CODE-BEHIND PAGE
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dd1.Items.Insert(0, "dd1-0");
dd1.Items[0].Value = "0";
dd1.Items.Insert(1, "dd1-1");
dd1.Items[1].Value = "1";
dd1.Items.Insert(2, "dd1-2");
dd1.Items[2].Value = "2";
}
else
{
dd1.SelectedIndex = dd1.Items.IndexOf(dd1.Items.FindByValue(Convert.ToString(Request.QueryString["dd1"])));
}
}
protected void RefreshPage(object o, EventArgs e)
{
Page.Response.Redirect("ExperimentalPage.aspx?"
+ "&dd1=" + dd1.SelectedValue);
}
THE PROBLEM PARTIALLY RESOLVED
See my answer
Ok, I have realized how to fix it. If anybody needs it, this is the solution.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dd1.Items.Insert(0, "dd1-0");
dd1.Items[0].Value = "0";
dd1.Items.Insert(1, "dd1-1");
dd1.Items[1].Value = "1";
dd1.Items.Insert(2, "dd1-2");
dd1.Items[2].Value = "2";
}
}
protected void RefreshPage(object o, EventArgs e)
{
Session["dd1"] = dd1.SelectedValue;
dd1.SelectedIndex = dd1.Items.IndexOf(dd1.Items.FindByValue(Convert.ToString(Session["dd1"])));
}
In this solution EnableViewState page property is True. There is no Page redirection. Query string is not used. Instead, Session variable is used. This is a good way to keep values during postback.