|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
add default row to DropDownListI am developing a CompoisiteControl. I have problems with adding a row in my DropDownList with the text "Select". The first time the side is opened the "Select" row is not there. When I do a postback the row appears. What do I do wrong? Thanks J Below is some code: protected override void CreateChildControls() { lblAttrAdditional.Text = "Add an attribute"; drAttrAdditional = new DropDownList(); drAttrAdditional.AutoPostBack = true; drAttrAdditional.SelectedIndexChanged += new EventHandler(drAttrAdditional_SelectedIndexChanged); this.Controls.Add(drAttrAdditional); //DataBind if (!Page.IsPostBack) { BindDropDownLists(); } } private void BindDropDownLists() { dsProducts.Tables["AttributeHeader"].DefaultView.RowFilter = "Default = '0'"; drAttrAdditional.DataSource = dsProducts.Tables["AttributeHeader"].DefaultView; drAttrAdditional.DataValueField = "AttrId"; drAttrAdditional.DataTextField = "Name"; drAttrAdditional.DataBind(); drAttrAdditional.Items.Insert(0, new ListItem("Select", "0")); } Try adding the ddl to the controls collection AFTER running the
BindDropDownLists() method. I seem to remember a similar problem that was solved in that way. -- Show quoteHide quoteRiki "Julia" <Ju***@discussions.microsoft.com> wrote in message news:11277CE7-F9DC-4308-AAC7-FB61E1DB1382@microsoft.com... > Hi > I am developing a CompoisiteControl. I have problems with adding a row in > my > DropDownList with the text "Select". The first time the side is opened the > "Select" row is not there. When I do a postback the row appears. > > What do I do wrong? > > Thanks > J > > Below is some code: > > protected override void CreateChildControls() > { > lblAttrAdditional.Text = "Add an attribute"; > drAttrAdditional = new DropDownList(); > drAttrAdditional.AutoPostBack = true; > drAttrAdditional.SelectedIndexChanged += new > EventHandler(drAttrAdditional_SelectedIndexChanged); > this.Controls.Add(drAttrAdditional); > //DataBind > if (!Page.IsPostBack) > { > BindDropDownLists(); > } > } > private void BindDropDownLists() > { > dsProducts.Tables["AttributeHeader"].DefaultView.RowFilter = > "Default = '0'"; > drAttrAdditional.DataSource = > dsProducts.Tables["AttributeHeader"].DefaultView; > drAttrAdditional.DataValueField = "AttrId"; > drAttrAdditional.DataTextField = "Name"; > drAttrAdditional.DataBind(); > drAttrAdditional.Items.Insert(0, new ListItem("Select", "0")); > } Perhaps it's cheating, but we often add the line in the stored procedure we
use to populate the control. For example... CREATE PROCEDURE [dbo].[sp_PopulateDropDown] AS SELECT 0 As IdentityID ,'--- Add New Line ---' As LineSpec UNION SELECT IdentityID ,ItemName As LineSpec FROM Items ORDER BY ItemName Asc -- Show quoteHide quoteThanks, CGW "Julia" wrote: > Hi > I am developing a CompoisiteControl. I have problems with adding a row in my > DropDownList with the text "Select". The first time the side is opened the > "Select" row is not there. When I do a postback the row appears. > > What do I do wrong? > > Thanks > J > > Below is some code: > > protected override void CreateChildControls() > { > lblAttrAdditional.Text = "Add an attribute"; > drAttrAdditional = new DropDownList(); > drAttrAdditional.AutoPostBack = true; > drAttrAdditional.SelectedIndexChanged += new > EventHandler(drAttrAdditional_SelectedIndexChanged); > this.Controls.Add(drAttrAdditional); > //DataBind > if (!Page.IsPostBack) > { > BindDropDownLists(); > } > } > private void BindDropDownLists() > { > dsProducts.Tables["AttributeHeader"].DefaultView.RowFilter = > "Default = '0'"; > drAttrAdditional.DataSource = > dsProducts.Tables["AttributeHeader"].DefaultView; > drAttrAdditional.DataValueField = "AttrId"; > drAttrAdditional.DataTextField = "Name"; > drAttrAdditional.DataBind(); > drAttrAdditional.Items.Insert(0, new ListItem("Select", "0")); > } Oops... the Order By should have been LineSpec
-- Show quoteHide quoteThanks, CGW "CGW" wrote: > Perhaps it's cheating, but we often add the line in the stored procedure we > use to populate the control. For example... > > CREATE PROCEDURE [dbo].[sp_PopulateDropDown] > > AS > > > > SELECT > 0 As IdentityID > ,'--- Add New Line ---' As LineSpec > > > UNION > > SELECT > IdentityID > ,ItemName As LineSpec > FROM > Items > ORDER BY > ItemName Asc > > -- > Thanks, > > CGW > > > "Julia" wrote: > > > Hi > > I am developing a CompoisiteControl. I have problems with adding a row in my > > DropDownList with the text "Select". The first time the side is opened the > > "Select" row is not there. When I do a postback the row appears. > > > > What do I do wrong? > > > > Thanks > > J > > > > Below is some code: > > > > protected override void CreateChildControls() > > { > > lblAttrAdditional.Text = "Add an attribute"; > > drAttrAdditional = new DropDownList(); > > drAttrAdditional.AutoPostBack = true; > > drAttrAdditional.SelectedIndexChanged += new > > EventHandler(drAttrAdditional_SelectedIndexChanged); > > this.Controls.Add(drAttrAdditional); > > //DataBind > > if (!Page.IsPostBack) > > { > > BindDropDownLists(); > > } > > } > > private void BindDropDownLists() > > { > > dsProducts.Tables["AttributeHeader"].DefaultView.RowFilter = > > "Default = '0'"; > > drAttrAdditional.DataSource = > > dsProducts.Tables["AttributeHeader"].DefaultView; > > drAttrAdditional.DataValueField = "AttrId"; > > drAttrAdditional.DataTextField = "Name"; > > drAttrAdditional.DataBind(); > > drAttrAdditional.Items.Insert(0, new ListItem("Select", "0")); > > }
DropDownList problem
Gridview and Filtering problem Need to keep source window open after Hyperlink is activated change web.config programmatically Hyperlink control NavigationUrl problem menu control will not expand (2.0) how to execute the DeleteCommand in code-behind? drop down list Threading in ASP.NET 2.0 web parts Gridview insert/update |
|||||||||||||||||||||||