Join 150,389 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,095 people online right now. Registration is fast and FREE... Join Now!
But no matter how many times I select a value it always holds the value of the first item in the list. Do I have to set AutoPostBack = true; to get this to work. If so that doesn't make a lot of sense, and Ive never heard of having to do that.
Well I tried setting AutoPostBack = true in this thing and even though the code to populate it is inside a if statement check to see if its a post back each time it posts back it reselects the first item in the list and doesn't keep the selection. Here is the code in the Page_Load event
csharp
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //make sure this is a valid request if (!(util.IsValidRequest(Request, "qmetro.com"))) { Response.Redirect("http://www.qmetro.com"); } else { //first we must decide which View we will be showing switch (Request.QueryString["action"].ToString()) { case "webMail": SetView(ActiveView.WebMail); break; case "csvFile": SetView(ActiveView.CSVFile); break; default: Response.Redirect("http://www.qmetro.com"); break; } } try { DataSet domains = util.BuildDataSet(Server.MapPath("TextFiles/supported_domains.txt"), "Domains", "\t"); supported_domain.DataSource = domains; supported_domain.DataTextField = domains.Tables[0].Columns[0].ToString(); supported_domain.DataValueField = domains.Tables[0].Columns[0].ToString(); supported_domain.DataBind(); supported_domain.Items.Insert(0, "[Select One]"); //now we will sort the DropDownList util.SortDropDownList(supported_domain); supported_domain.SelectedIndex = -1; } catch { Response.Write(util.Message); } btnWriteMessage.Attributes.Add("onclick", "return CheckContacts();return false;"); contactsList.Visible = false; btnWriteMessage.Text = "Continue ->"; messagePanel.Visible = false; Email.Focus(); } }
This is driving me batty as this should be an easy thing to accomplish. I don't think Ive ever ran into something like this
I tried that as well and it always returns psychocoder@[Select One] no matter what I try. This is insane, I'm wondering if someone how the server I'm working on is causing this, but cant figure out what it could be
This is getting really frustrating. I set AutoPostBack = "true" on my DropDownList and on the post back it loses it's selection, even though I'm only populating it if IsPostBack == false. I have EnableViewState = "true" on both the page & the DropDownList, nothing seems to be helping here.
Ironically I have been working on a very similar problem at work. The company's portal has a bug where the drop down list is doing the same thing. I am currently setting up a sample project right now to duplicate the problem and attempt to determine how to stop it.
I will get back to you as soon as I can nail it down.
Well heres a weird one for you Jay, I got it working in IE7, but in Firefox I get an "Invalid postback or callback argument". Any idea what thats about?
It's just a utilities class, it handles several routine functions & methods that are used more than once, so instead of typing the code everything I need it I created a utilities class. I here is the methods in that class that sorts the DDL and populates it from the text file:
BuildDataSet:
csharp
#region BuildDataSet /// <summary> /// method to read a text file into a DataSet /// </summary> /// <param name="file">file to read from</param> /// <param name="tableName">name of the DataTable we want to add</param> /// <param name="delimeter">delimiter to split on</param> /// <returns>a populated DataSet</returns> public DataSet BuildDataSet(string file,string tableName,string delimeter) { //create our DataSet DataSet domains = new DataSet(); //add our table domains.Tables.Add(tableName); try { //first make sure the file exists if (File.Exists(file)) { //create a StreamReader and open our text file StreamReader reader = new StreamReader(file); //read the first line in and split it into columns string[] columns = reader.ReadLine().Split(delimeter.ToCharArray()); //now add our columns (we will check to make sure the column doesnt exist before adding it) foreach (string col in columns) { //variable to determine if a column has been added bool added = false; string next = ""; //our counter int i = 0; while (!(added)) { string columnName = col; //now check to see if the column already exists in our DataTable if (!(domains.Tables[tableName].Columns.Contains(columnName))) { //since its not in our DataSet we will add it domains.Tables[tableName].Columns.Add(columnName, typeof(string)); added = true; } else { //we didnt add the column so increment out counter i++; } } } //now we need to read the rest of the text file string data = reader.ReadToEnd(); //now we will split the file on the carriage return/line feed //and toss it into a string array string[] rows = data.Split("\r".ToCharArray()); //now we will add the rows to our DataTable foreach (string r in rows) { string[] items = r.Split(delimeter.ToCharArray()); //split the row at the delimiter domains.Tables[tableName].Rows.Add(items); } } else { throw new FileNotFoundException("The file " + file + " could not be found"); }
//now return the DataSet return domains; } #endregion
SortDropDownList:
csharp
#region SortDropDownList /// <summary> /// method to sort a DropDownList /// </summary> /// <param name="ddl">DropDownList to sort</param> public void SortDropDownList(DropDownList ddl) { //create a ListItem array the size of the items //in your DropDownList ListItem[] sorted = new ListItem[ddl.Items.Count]; //loop through all the items in your ListItem array for (int i = 0; i < sorted.Length; i++) { //resize the array on each iteration Array.Resize(ref sorted, i); //add the current index to the array sorted[i] = ddl.Items[i]; } //call Array.Sort to sort your ListItem array Array.Sort(sorted); //remove all items from the DropDownList ddl.Items.Clear(); //add the sorted items to the DropDownList ddl.Items.AddRange(sorted); } #endregion
Well I gotta say that I set up a test project and I am not having any problem getting the value in the SelectedIndexChanged method after Post Back. The only thing I didn't implement was your utilities class.
Are you instantiating the DropDownList in code or through the designer?