Jump to content

Dropdownlist problem


cyrus_
 Share

Recommended Posts

Hi, I have been working with my project for 2 weeks but i have failed everytime, The problem is i want to catch the selecteditem in my dropdownlist but it is just the first item everytime,here is my code: protected override void CreateChildControls() { DDL = new DropDownList(); DDL.Load += new EventHandler(DDLLoad); DDL.SelectedIndexChanged += new EventHandler(DDLItemChanged); Controls.Add(DDL); } protected void DDLLoad(object sender, EventArgs e) { DS = GetChannels(); if (DS.Tables.Count > 0 ) { DDL.DataSource = DS; DDL.DataTextField = "title"; DDL.DataValueField = "title"; DDL.DataBind(); } else { MessageLBL.Text = "Unable to connect to the database."; } } protected void DDLItemChanged(object sender, EventArgs e) { string mytitle = DDL.SelectedValue; MessageLBL.Text = mytitle; //here it is always the first item }

Link to comment
Share on other sites

When you select an item in the DDL does it postback?If it does it is reloading before catching the value, that is why you always get the first value.Try this

protected override void CreateChildControls(){  if(!IsPostBack)  {    DDL = new DropDownList();    DDL.Load += new EventHandler(DDLLoad);    DDL.SelectedIndexChanged += new EventHandler(DDLItemChanged);    Controls.Add(DDL);  }}

Link to comment
Share on other sites

When you select an item in the DDL does it postback?[/code]Thank you ,I am newbe , i have read a lot about postback but i have not uderstood correctly,when you say if it is be postedback, what do you mean and how i can now that it postedback?when i click on add_button or choose on of the items in my dropdownlist my page is uploads and the dropdownlist shows the first item again but it contains the others too.thank you again.
Link to comment
Share on other sites

so if you use this code it should work

protected override void CreateChildControls(){ if(!IsPostBack) {   DDL = new DropDownList();   DDL.Load += new EventHandler(DDLLoad);   DDL.SelectedIndexChanged += new EventHandler(DDLItemChanged);   Controls.Add(DDL); }}

Link to comment
Share on other sites

so if you use this code it should work
protected override void CreateChildControls(){ if(!IsPostBack) {   DDL = new DropDownList();   DDL.Load += new EventHandler(DDLLoad);   DDL.SelectedIndexChanged += new EventHandler(DDLItemChanged);   Controls.Add(DDL); }}

thanks again, when i use your code my drondownlist is empty first time the page will be refresh, (It has items just first time)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...