Welcome to Dream.In.Code
Become an Expert!

Join 150,075 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,792 people online right now. Registration is fast and FREE... Join Now!




Question about SelectedIndex Changed on DropDownList

2 Pages V  1 2 >  
Reply to this topicStart new topic

Question about SelectedIndex Changed on DropDownList

PsychoCoder
29 Feb, 2008 - 09:03 AM
Post #1

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Im trying to get the selected value of my DropDownList with this code


csharp

protected void supported_domain_SelectedIndexChanged(object sender, EventArgs e)
{
domain = supported_domain.SelectedItem.Text;
}



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.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Question About SelectedIndex Changed On DropDownList
29 Feb, 2008 - 10:14 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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 crazy.gif
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Question About SelectedIndex Changed On DropDownList
29 Feb, 2008 - 11:19 AM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
You might try using the SelectedValue property.

CODE
domain = supported_domain.SelectedValue

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Question About SelectedIndex Changed On DropDownList
29 Feb, 2008 - 11:21 AM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Question About SelectedIndex Changed On DropDownList
29 Feb, 2008 - 11:30 AM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Hmm..well I will have to wait till I get home from work. Then I will set up a sample project and look into this further for you.

Sorry I can't be of more help at the moment.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Question About SelectedIndex Changed On DropDownList
29 Feb, 2008 - 10:22 PM
Post #6

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
So, anyone got any ideas on why my SelectedIndexChanged Event isn't working? This is really driving me nuts crazy.gif
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Question About SelectedIndex Changed On DropDownList
1 Mar, 2008 - 10:14 AM
Post #7

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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.

Has anyone else ran into this problem before?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Question About SelectedIndex Changed On DropDownList
1 Mar, 2008 - 03:37 PM
Post #8

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Question About SelectedIndex Changed On DropDownList
1 Mar, 2008 - 05:17 PM
Post #9

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Question About SelectedIndex Changed On DropDownList
1 Mar, 2008 - 05:49 PM
Post #10

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
What is the util object you are using? Is that just a class you created to handle sorting of the DDL?
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Question About SelectedIndex Changed On DropDownList
1 Mar, 2008 - 05:55 PM
Post #11

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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");
}

}
catch (FileNotFoundException ex)
{
_message = ex.Message;
return null;
}
catch (Exception ex)
{
_message = ex.Message;
return null;
}

//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

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Question About SelectedIndex Changed On DropDownList
1 Mar, 2008 - 08:51 PM
Post #12

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
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?

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 11:11PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month