Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a web form and I want ListBox/DropDownList to keep its value when SelectedIndexChanged.The problem is when page is refreshed it goes back to the top of index.Is it possible to make it work like ComboBox in Window form? Because i want to fetch data from database related to that item which is present in ListBox/DropDownList.I have populated Listbox/DropDownList controls dynamically.

Here is an Example of what i exactly want...

https://lh4.googleusercontent.com/-_5Y2awm64PI/UtYNS3Cp4mI/AAAAAAAABKQ/PYLTwJ8QL-o/w346-h376/Fullscreen+capture+1152014+94612+AM.bmp.jpg[^]
Posted

Pls take care of postback in the page load..


C#
protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               DropDownList1.DataSource = yoursource;
               DropDownList1.DataBind();
           }
       }
 
Share this answer
 
It is happening because you are loading the dropdownlist/Listbox every time when page is called.You have to use ISpostback property i.e. You have to load dropdownlist for the first time when page is called.
Write the below code in pageload event

Page_load Event ()
{
if (!ispostback)
{
load your dropdownlist /Listboxlist
}
}

I think this should work . Try and let me know
 
Share this answer
 
Comments
uditCsharp 15-Jan-14 3:25am    
U can prevent page load with this but how would you get value of selectedindex eachtime on selectedindexchanged event without reloading page.
Here is a small example that you could follow

<asp:dropdownlist id="DropDownList1" runat="server" xmlns:asp="#unknown">
onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">


Following is in .cs

C#
public partial class DropDownListSelectedIndex : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownList1.DataSource = DataClass2.getData();
                DropDownList1.DataTextField = "s";
                DropDownList1.DataValueField = "s2";
                DropDownList1.DataBind();
            }

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList dptList = (DropDownList)sender;
            Label1.Text = dptList.SelectedIndex.ToString();
        }
    }
//Following class is used just to get the data
    public class DataClass2
    {
        public string s { get; set; }
        public string s2 { get; set; }
        public static List<dataclass2> getData()
        {
            List<dataclass2> dataLst = new List<dataclass2>();
            for (int i = 1; i <= 5; i++)
            {
                DataClass2 data = new DataClass2();
                data.s = "S" + i;
                data.s2 = "M" + i;
                dataLst.Add(data);
            }
            return dataLst;
        }
    }</dataclass2></dataclass2></dataclass2>
 
Share this answer
 
Thank You all for Your response but i figured out the most suitable technique... I just needed to create a new eventhandler aftr page load referrenced to DropDownlist Selectedindexchanged event and Set DropDownlist Autopostback Proprty to True..


Ex:

Page_load()
{
if(!IsPostback)
{
//Some Code//
//Databound //
}

DropDownList1.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged);
}
 
Share this answer
 
v4

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