Go Back   DeveloperBarn Forums > Programming & Scripting > .Net Development

Sponsored Links

Discuss "How do you add a static item to a dynamic dropdownlist" in the .Net Development forum.

.Net Development - Learn about the Microsoft.Net framework and how to create powerful web-based (ASP.net) and client-based (Windows Forms) applications utilizing various languages such as C#, VB.Net, J# and others.


Reply « Previous Thread | Next Thread »
 
LinkBack Thread Tools Display Modes
  #1  
Old July 4th, 2008, 03:55 AM
Shem's Avatar
Barn Enthusiast

 
Join Date: Mar 2008
Posts: 261
Thanks: 30
Thanked 5 Times in 5 Posts
Rep Power: 1
Shem is on a distinguished road
Default How do you add a static item to a dynamic dropdownlist

How does one achieve this?
basically i need to add a "Select Here" to my DDL, and it must be selected.

Shem
Reply With Quote
Sponsored Links
  #2  
Old July 4th, 2008, 04:04 AM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

Assuming you're using a datasource to bind to the DDL use something like this after you've bound the data to the ddl:-
Code:
ddl_id.items.insert(0, New ListItem("Please Select",""))

Last edited by richyrich; July 4th, 2008 at 04:11 AM.
Reply With Quote
The Following User Says Thank You to richyrich For This Useful Post:
Shem (July 4th, 2008)
  #3  
Old July 4th, 2008, 04:14 AM
Shem's Avatar
Barn Enthusiast

 
Join Date: Mar 2008
Posts: 261
Thanks: 30
Thanked 5 Times in 5 Posts
Rep Power: 1
Shem is on a distinguished road
Default

Or alternatively if your using the DataSourceControl, set the
AppendDataBoundItems to True of your DDL and then just add
Code:
<asp:DropDownList ID="ProjectDDL" runat="server" AutoPostBack="True" DataSourceID="ProjectDataSource" DataTextField="Name" DataValueField="Idkey" Visible="false" AppendDataBoundItems="True">
                        <asp:ListItem Text="Select Project" Value="0" Selected="True"></asp:ListItem>
                    </asp:DropDownList>
to your DDL

Shem
Reply With Quote
  #4  
Old July 4th, 2008, 04:17 AM
Shem's Avatar
Barn Enthusiast

 
Join Date: Mar 2008
Posts: 261
Thanks: 30
Thanked 5 Times in 5 Posts
Rep Power: 1
Shem is on a distinguished road
Default

Quote:
Originally Posted by richyrich View Post
Assuming you're using a datasource to bind to the DDL use something like this after you've bound the data to the ddl:-
Code:
ddl_id.items.insert(0, New ListItem("Please Select",""))
this works 100% too, and probably the better way to do it
Thanks RR
Reply With Quote
  #5  
Old July 4th, 2008, 04:21 AM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

Quote:
Originally Posted by Shem View Post
Or alternatively if your using the DataSourceControl, set the
AppendDataBoundItems to True of your DDL and then just add
Code:
<asp:DropDownList ID="ProjectDDL" runat="server" AutoPostBack="True" DataSourceID="ProjectDataSource" DataTextField="Name" DataValueField="Idkey" Visible="false" AppendDataBoundItems="True">
                        <asp:ListItem Text="Select Project" Value="0" Selected="True"></asp:ListItem>
                    </asp:DropDownList>
to your DDL

Shem
You answered your own question Shem!! LOL

With this method I'm not sure what would happen on postback. Does it keep the Select Project item selected?
Reply With Quote
  #6  
Old July 4th, 2008, 04:26 AM
Shem's Avatar
Barn Enthusiast

 
Join Date: Mar 2008
Posts: 261
Thanks: 30
Thanked 5 Times in 5 Posts
Rep Power: 1
Shem is on a distinguished road
Default

It keeps the selected item selected, ie. doesn't just stick to "Select here".

BTW: your method isn't working for me, I only tested your in conjuction to mine,
basically your will also only work when AppendDataBoundItems = true

Shem
Reply With Quote
  #7  
Old July 4th, 2008, 04:27 AM
Shem's Avatar
Barn Enthusiast

 
Join Date: Mar 2008
Posts: 261
Thanks: 30
Thanked 5 Times in 5 Posts
Rep Power: 1
Shem is on a distinguished road
Default

sh!t....

If you click on "Select here", it adds it to the collection, so you end up with multiple "Select Here" options
Reply With Quote
  #8  
Old July 4th, 2008, 04:46 AM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

Quote:
Originally Posted by Shem View Post
sh!t....

If you click on "Select here", it adds it to the collection, so you end up with multiple "Select Here" options
This is what I do and don't have any problems:-
.aspx
Code:
<asp:dropdownlist id="ddl_id" runat="server" />
.aspx.vb
Code:
sub Page_Load()
 
if not ispostback then
 
      build_ddl_id()
 
end if
 
sub build_ddl_id()
 
' do all your datasource stuff first
 ddl_id.datasource = your_datasource
 ddl_id.databind
 
 ddl_id.items.insert(0, New ListItem("Select Project", ""))
 ddl_id.selectedvalue=""
 
end sub
You only build objects on the page load, not on postback. The viewstate should take care of that. I've not tried having listitems in my .aspx code and databinding to the same ddl. If I use a datasource, I build the whole thing in code behind. I don't know if using list items and a datasourec are mutually exclusive. Maybe it messes with the viewstate.

Obviously your datasource can be a datareader or list of from a class.

Hope that helps.

Last edited by richyrich; July 4th, 2008 at 04:51 AM.
Reply With Quote
  #9  
Old July 4th, 2008, 04:51 AM
Shem's Avatar
Barn Enthusiast

 
Join Date: Mar 2008
Posts: 261
Thanks: 30
Thanked 5 Times in 5 Posts
Rep Power: 1
Shem is on a distinguished road
Default

ok, but i am using the datasource control, so how do i build/bind on postback?

Shem
Reply With Quote
  #10  
Old July 4th, 2008, 05:02 AM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

Quote:
Originally Posted by Shem View Post
ok, but i am using the datasource control, so how do i build/bind on postback?

Shem
Ahh, yes....I forgot you were using a control as the datasource...

What happens if you remove the selected="true" from the Select Project item?
Reply With Quote
Reply

  DeveloperBarn Forums > Programming & Scripting > .Net Development

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[ASP.Net/VB.Net] Programmatically Select Item in ListBox or DropDownList jmurrayhead Code Samples 0 June 27th, 2008 10:05 AM
[ASP.Net/VB.Net] Programmatically Add Item to Validation Summary jmurrayhead Code Samples 0 March 20th, 2008 09:41 AM


All times are GMT -4. The time now is 04:16 PM.



Content Relevant URLs by vBSEO 3.2.0