Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Aug 2005
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Databind Dropdownlist problem

    I can't seem to remember how to fix this one problem. I've got a dropdownlist that is filled from a database as seen below:

    Code:
    qryStr="select id, topping from toppings";
    			OleDbCommand cmd = new OleDbCommand(qryStr, cn);
    			OleDbDataReader rdr=cmd.ExecuteReader();
    			
    			selectTopping.DataSource = rdr;
    			selectTopping.DataTextField = "topping";
    			selectTopping.DataValueField = "id";
    			selectTopping.DataBind();			
    			rdr.Close();
    The problem is when they've entered all the info and submit it it ignores what had been selected. I know I've fixed this problem before, but I can't remember how and can't find the code where I did. I thought I did it by something like this:
    Code:
    int selIndex=0;
    			if (!IsPostBack){
    				selIndex=selectTopping.SelectedIndex;			}
    qryStr="select id, topping from toppings";
    			OleDbCommand cmd = new OleDbCommand(qryStr, cn);
    			OleDbDataReader rdr=cmd.ExecuteReader();
    			
    			selectTopping.DataSource = rdr;
    			selectTopping.DataTextField = "topping";
    			selectTopping.DataValueField = "id";
    			selectTopping.DataBind();			
    			rdr.Close();
    			selectTopping.SelectedIndex=selIndex;
    But that seems to be having no effect. I put in Response.Write in the if statement to find out what the SelectedIndex value was and it seems to always think it's -1. I can't seem to find a way to make it remember what I select.

    Any help would be appreciated.

    Brett Bretterson

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Hi,
    Set the AutoPostBack property of the drop down list to true.

    Good luck;

  • #3
    New Coder
    Join Date
    Aug 2005
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts
    But if I set its autopostback to true then when I change it it will automatically post back, which I don't want. I want to be able to change what I've selected and have the correct selected value be taken when I post back later.

  • #4
    New Coder
    Join Date
    Aug 2005
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've got a way to do it using javascript, but not without. I'd still like anyone's suggestions.

    Brett Bretterson

  • #5
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    I don't think I understand. When you submit your form, is it giving you the correct value for the drop down list? If you have the autopostback set to true, and then check if(!IsPostBack) bind data. You should OK.

    Good luck;

  • #6
    New Coder
    Join Date
    Oct 2004
    Posts
    64
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I am having the same problem, I think.

    I have a dropdown list that has people's names as the list items and the values are their login codes. For example here is a dropdown list:

    John Doe (jdoe)
    Bob Smith (bsmith)
    Jane Jones (jjones)

    I have the dropdown set to auto post back, and I want their names and login codes to be populated into some text boxes when their name is chosen from the list.

    However it seems that no matter which one I choose from the list, the textbox is always filled with John Doe's information. It doesn't seem to know which one I selected.

    Here's my (abbreviated) code:

    Code:
    protected System.Web.UI.WebControls.DropDownList dropNames;
    protected System.Web.UI.WebControls.TextBox txtName;
    protected System.Web.UI.WebControls.TextBox txtLogin;
    
    dropNames.Items.Add(new ListItem("John Doe", "jdoe"));
    dropNames.Items.Add(new ListItem("Bob Smith", "bsmith"));
    dropNames.Items.Add(new ListItem("Jane Jones", "jjones"));
    
    private void dropNames_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    	//populate the text boxes with the information in here.
    	txtName.Text = dropNames.SelectedItem.Text;
    	txtLogin.Text = dropNames.SelectedItem.Value;
    }

  • #7
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    This seems to be working for me:
    Code:
    private void Page_Load(object sender, System.EventArgs e)
    {
    	if(!IsPostBack)
    	{
    		dropNames.Items.Add(new ListItem("John Doe", "jdoe"));
    		dropNames.Items.Add(new ListItem("Bob Smith", "bsmith"));
    		dropNames.Items.Add(new ListItem("Jane Jones", "jjones"));
    	}
    }
    
    private void dropNames_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    	//populate the text boxes with the information in here.
    	txtName.Text = dropNames.SelectedItem.Text;
    	txtLogin.Text = dropNames.SelectedItem.Value;
    }
    Assuming you have autopostback set to true(as you've stated) I don't see any problems with it.

  • #8
    New Coder
    Join Date
    Oct 2004
    Posts
    64
    Thanks
    2
    Thanked 0 Times in 0 Posts
    This works now. Thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •