|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
DropDownlist problemeverytime, 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 } You have to call the the CreateChildControls during the page initialization
phase in order for the ViewState to be loaded upon postback and the correct events get triggered. But if you call the CreateChildControls during the page.load or any later stage then the event will never fire upon postback. For an overview of the page's life cycle: http://msdn2.microsoft.com/en-us/library/ms178472(VS.80).aspx Show quoteHide quote "cyrus" wrote: > 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 > } First try modifying this in CreateChildControls:
if(!Page.IsPostback) DDL.Load += new EventHandler(DDLLoad); That should work if you have view state enabled on your control and the page because the items will be fetched from the view state every time. If the above method does not work then try modifying DDLoad, inside the first part of the if statement. string selected_value = DDL.SelectedValue; DDL.DataSource = DS; DDL.DataTextField = "title"; DDL.DataValueField = "title"; DDL.DataBind(); if(DDL.Items.FindByValue(selected_value) != null) DDL.SelectedValue = selected_value; Show quoteHide quote "cyrus" wrote: > 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 > }
GridView w/ ObjectDataSource w/ Business object layer
GridView, ObjectDataSOurce, and enum parameter for select Gridview custom sort - works but always sorts ascending Web UserControl (ascx) and UrlProperty attribute Perform insert to gridview/table Datalist formatting questions Adding client side code to RadioButtonList control item??? DataGrid1.DataSource = ds.Tables(2) Treeview SelectedNodeChanged event not firing GridView custom control and post back issues |
|||||||||||||||||||||||