Question about SelectedIndex Changed on DropDownList

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 29911 Views - Last Post: 20 December 2010 - 09:42 AM Rate Topic: -----

#1 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Question about SelectedIndex Changed on DropDownList

Posted 29 February 2008 - 10:03 AM

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


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.

Is This A Good Question/Topic? 0
  • +

Replies To: Question about SelectedIndex Changed on DropDownList

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Question about SelectedIndex Changed on DropDownList

Posted 29 February 2008 - 11:14 AM

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


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:
Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Question about SelectedIndex Changed on DropDownList

Posted 29 February 2008 - 12:19 PM

You might try using the SelectedValue property.

domain = supported_domain.SelectedValue

Was This Post Helpful? 1

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Question about SelectedIndex Changed on DropDownList

Posted 29 February 2008 - 12:21 PM

I tried that as well and it always returns [email protected][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
Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Question about SelectedIndex Changed on DropDownList

Posted 29 February 2008 - 12:30 PM

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.
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Question about SelectedIndex Changed on DropDownList

Posted 29 February 2008 - 11:22 PM

So, anyone got any ideas on why my SelectedIndexChanged Event isn't working? This is really driving me nuts :crazy:
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Question about SelectedIndex Changed on DropDownList

Posted 01 March 2008 - 11:14 AM

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?
Was This Post Helpful? 0
  • +
  • -

#8 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Question about SelectedIndex Changed on DropDownList

Posted 01 March 2008 - 04:37 PM

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.
Was This Post Helpful? 0
  • +
  • -

#9 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Question about SelectedIndex Changed on DropDownList

Posted 01 March 2008 - 06:17 PM

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?
Was This Post Helpful? 0
  • +
  • -

#10 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Question about SelectedIndex Changed on DropDownList

Posted 01 March 2008 - 06:49 PM

What is the util object you are using? Is that just a class you created to handle sorting of the DDL?
Was This Post Helpful? 0
  • +
  • -

#11 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Question about SelectedIndex Changed on DropDownList

Posted 01 March 2008 - 06:55 PM

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:
#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:
#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


Was This Post Helpful? 0
  • +
  • -

#12 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Question about SelectedIndex Changed on DropDownList

Posted 01 March 2008 - 09:51 PM

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?
Was This Post Helpful? 0
  • +
  • -

#13 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Question about SelectedIndex Changed on DropDownList

Posted 01 March 2008 - 11:07 PM

Actually I just typed the HTML like <asp:DropDownList.../>. I'm ready to give up and just try another route because I'm tired of fighting with it. This is the only DropDownList I'm having a problem with, all the others on the rest of the pages work like they're supposed to. I don't ever look at pages in designer view, always in source view.

This post has been edited by PsychoCoder: 01 March 2008 - 11:08 PM

Was This Post Helpful? 0
  • +
  • -

#14 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Question about SelectedIndex Changed on DropDownList

Posted 01 March 2008 - 11:20 PM

My question was more related to the code-behind. But that is not the case, so not the issue.

I am not sure what could be causing the problem. If you want to send me the project I can take a closer look.
Was This Post Helpful? 0
  • +
  • -

#15 dilipv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-February 08

Re: Question about SelectedIndex Changed on DropDownList

Posted 06 March 2008 - 01:33 AM

View PostPsychoCoder, on 1 Mar, 2008 - 06:55 PM, said:

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:
#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:
#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



hi PsychoCoder,

i saw your given solution it has really cleared all things.i reall y help me to solve my query as well.

Thank you
Jitesh
programmer
Sharepoint Consulting
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2