Question

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

Was it helpful?

Solution

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.

OTHER TIPS

When you select an item from the dropdown, you will hit the Page_Load() before the RefreshPage() method

That means you will execute this line before the RefreshPage method:

dd1.SelectedIndex = dd1.Items.IndexOf(dd1.Items.FindByValue(Convert.ToString(Request.QueryString["dd1"])));

Convert.ToString(Request.QueryString["dd1"])) will evaluate to null, thus SelectedIndex will be set to 0.

If you want to make this work you should try the following:

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";

    if (!string.IsNullOrEmpty(Request.QueryString["dd1"]))
    {
        dd1.SelectedIndex = dd1.Items.IndexOf(dd1.Items.FindByValue(Convert.ToString(Request.QueryString["dd1"])));
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top