add default row to DropDownList

J

Julia

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"));
}
 
R

Riki

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.
 
C

CGW

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
 
C

CGW

Oops... the Order By should have been LineSpec
--
Thanks,

CGW


CGW said:
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 said:
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"));
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top