Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I force a different initial value on a DropDownList?

Status
Not open for further replies.

SCantoria

Programmer
Sep 23, 2005
100
US
My DropDownList control is getting the information from an ObjectDataSource. I need the initial item showing on the DropDownList to be "Select an item" which is not in the database. How do I do this?

See my code below. Thanks.


<asp:DropDownList ID="ddlCP" runat="server"
DataSourceID="odsCP"
DataTextField="Project Name"
DataValueField="Project Name"
AutoPostBack="True"
OnSelectedIndexChanged="ddlCP_SelectedIndexChanged" >
<asp:ListItem Selected="True" Text=" Select an item..." Value=" Select an item..."></asp:ListItem>
</asp:DropDownList>



<asp:ObjectDataSource ID="odsCP" runat="server"
OldValuesParameterFormatString="original_{0}"
SelectMethod="GetUserCP"
TypeName="IRISTableAdapters.vwUserCPTableAdapter">
<SelectParameters>
<asp:ControlParameter ControlID="txtCreator" Name="username" PropertyName="Text"
Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
 
i believe there is a property on the dropdown list control like appenddataitems or something like that. set this to true and you should be good to go.

btw, I would not use datasource controls you; cannot debug them. if you the declarative markup to the code behind you can at least step through the code to debug. moving the code out of the code behind and into a separate class altogether will allow you to run automated tests easier, if you move in that direction.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Thanks. I learned how to use the OnDataBound event. For those who care to know, I resolved my issue by using the OnDataBound event.

protected void GridView1_OnDataBound(object sender, EventArgs e)
{
DropDownList1.Items.Insert(0,"Select an item...");
}


Thanks for everyone's advice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top