Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi people,

Im a little new to ASP.NET and I'm trying to get a asp:dropdownlist to remember the the selected item on autopostback. However it doesnt. I have looked around online and many sites suggest placing the code to populate the dropdownlist inside a if(!Page.IsPostBack) block. However this still doesnt work. Any ideas? Here is my code

XML
<asp:DropDownList
    ID="dpdAdverts"
    runat="server"
    AutoPostBack="true" EnableViewState="true" ></asp:DropDownList>


and

C#
if (!Page.IsPostBack)
{
    PopulateAdvertsList();
}


Many thanks!
Posted
Comments
bbirajdar 4-Mar-13 11:00am    
Use a ViewState variable to remember the selected index and assign the selectedIndex after the bind logic.

1 solution

Hi ,as per your query ,may be it will help you.
On Default.aspx page:
C#
<asp:dropdownlist id="ddlSelctIntiger" runat="server" enableviewstate="true" autopostback="true" xmlns:asp="#unknown">
            onselectedindexchanged="OnddlSelctIntigerIndexChanged">
        </asp:dropdownlist>

on Default.cs Page:
C#
protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               BindDataToDropDownList();
           }
       }

       private void BindDataToDropDownList()
       {
           ddlSelctIntiger.Items.Add("Five");
           ddlSelctIntiger.Items.Add("Six");
           ddlSelctIntiger.Items.Add("Seven");

       }


       protected void OnddlSelctIntigerIndexChanged(object sender, EventArgs e)
       {
           string temp = ddlSelctIntiger.SelectedItem.Text;
           Response.Write(temp);
       }


and try to solve your issue.
Thanks
JmD
 
Share this answer
 
v2
Comments
Adam92 5-Mar-13 7:05am    
Hi there,

Thankyou for your response, but this does not resolve the problem

Adam
giri001 6-Mar-13 3:18am    
Hi Adams,
Sorry for above one,Use this...may be it will help u....
and on .aspx page:
<body>
<form id="form1" runat="server">
<asp:DropDownList ID="ddlSelctIntiger" runat="server"
AutoPostBack="True"
EnableViewState="True" ontextchanged="ddlSelctIntiger_TextChanged" >

<div>
<asp:HiddenField runat="server" ID="hidSelectedvalue" />
</div>
</form>
</body>
and on .cs page:
private static string temp = "";
private static string tempOld = "";
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(temp);
tempOld = ddlSelctIntiger.Text;
if (!IsPostBack)
{
BindDataToDropDownList();
}
}

private void BindDataToDropDownList()
{
ddlSelctIntiger.Items.Add("One");
ddlSelctIntiger.Items.Add("Six");
ddlSelctIntiger.Items.Add("Seven");
ddlSelctIntiger.SelectedItem.Selected = true;
temp = ddlSelctIntiger.SelectedItem.Text;
}

protected void ddlSelctIntiger_TextChanged(object sender, EventArgs e)
{
temp = tempOld;
}

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