cyrus_ Posted March 16, 2006 Share Posted March 16, 2006 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 More sharing options...
aspnetguy Posted March 16, 2006 Share Posted March 16, 2006 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 More sharing options...
cyrus_ Posted March 16, 2006 Author Share Posted March 16, 2006 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 More sharing options...
aspnetguy Posted March 16, 2006 Share Posted March 16, 2006 A postback is basically a page refresh.When you press your button the page refreshes doesn't it? Link to comment Share on other sites More sharing options...
cyrus_ Posted March 16, 2006 Author Share Posted March 16, 2006 A postback is basically a page refresh.When you press your button the page refreshes doesn't it?<{POST_SNAPBACK}> yes it is Link to comment Share on other sites More sharing options...
aspnetguy Posted March 17, 2006 Share Posted March 17, 2006 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 More sharing options...
cyrus_ Posted March 17, 2006 Author Share Posted March 17, 2006 mistake Link to comment Share on other sites More sharing options...
cyrus_ Posted March 17, 2006 Author Share Posted March 17, 2006 so if you use this code it should workprotected override void CreateChildControls(){ if(!IsPostBack) { DDL = new DropDownList(); DDL.Load += new EventHandler(DDLLoad); DDL.SelectedIndexChanged += new EventHandler(DDLItemChanged); Controls.Add(DDL); }} <{POST_SNAPBACK}> 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 More sharing options...
aspnetguy Posted March 17, 2006 Share Posted March 17, 2006 is it completely necessary to create the DDL dynamically??? It makes things more complicated...if it doesn't you should not do it dynamically. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now