DeveloperBarn Forums

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

Discuss "Dropdown list" 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 (permalink)  
Old May 27th, 2008, 06:51 AM
Shem's Avatar
Contributing Member

 
Join Date: Mar 2008
Posts: 192
Thanks: 24
Thanked 5 Times in 5 Posts
Rep Power: 1
Shem is on a distinguished road
Default Dropdown list

Hi Guys

Ok i gotta build my first dropdown list, which in turn will populate another
dropdown list, but lets get the dropdown list right first

I've read that it can be databound? so can i populate the list from a method?
i.e. run the query grab the results in a method of a class and bind my dropdown
list control to this method?

Shem
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old May 27th, 2008, 07:18 AM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 331
Thanks: 23
Thanked 26 Times in 26 Posts
Blog Entries: 1
Rep Power: 1
richyrich is on a distinguished road

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

It's not something I've done, but I see no reason why you couldn't, you just need to set it as a type that can populate a dropdown.

To populate a dropdownlist in code behind, I'd have something like this:-
Code:
sub build_ddl_myddl()
 
Dim conn as new mysqlconnection(connString)
Dim mycommand as new mysqlcommand("SELECT fld_id,fld_text FROM tbl_name")
mycommand.connection = conn
Dim rs as mysqldatareader
try
      conn.open
      rs = mycommand.executereader
      if rs.hasrows then
         my_ddlid.datasource = rs
         my_ddlid.datavaluefield = "fld_id"
         my_ddlid.datatextfield = "fld_text"
         my_ddlid.databind()
      end if
      rs.close
catch ex as exception
      response.write(ex.message.tostring)
finally
      mycommand.dispose
      conn.close
end try
 
end sub
Like I say, I've not tried it in the way you're trying. I guess you could return the function as a dropdownlist, create the whole thing in your function and then on your page just have:-
Code:
my_ddlid = myBLL.getdropdown
Hope that helps

I suspect J will have a better way of doing it, but in theory what you're trying to do should be no problem. It's just a matter of what you return the data form you function as to manipulate it into a DDL.
Reply With Quote
  #3 (permalink)  
Old May 27th, 2008, 07:29 AM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 537
Thanks: 14
Thanked 40 Times in 39 Posts
Blog Entries: 2
Rep Power: 1
jmurrayhead will become famous soon enough

Awards Showcase
Microsoft Windows Microsoft .Net Microsoft SQL Server Classic ASP 
Total Awards: 4

Default



Let's say you wanted to populate your dropdown with a list of users in the system. You already have the users class with the appropriate objects. What you can do is create a new class that is a collection, or List Of the User class:

Code:
Namespace Company.AppName.BO
    Inherits List(Of User)
    
    Public Class UserList
        Public Sub New()
 
        End Sub
    End Class
End Namespace
Notice "List(Of User)". This says, make a list from the class "User".

Now, you can take a function that gets the data to populate this list and return the UserList to bind to the dropdown.

So, in the BLL, you'll have this function: GetUserList() As UserList

In the DAL, the same thing: GetUserList() As UserList

Then just bind it to the DropDown

For example of the list, see here: Building Layered Web Applications with Microsoft ASP.NET 2.0 - Part 1 - Imar.Spaanjaars.Com

Download the sample file and check the "App_Code\BusinessObject\Collections" folder.
__________________
jmurrayhead
Did I help you out? Make me popular by clicking the icon!

If you found a post helpful, please click the button in the lower right-hand corner of the post.

Powered by ASP.Net
Reply With Quote
  #4 (permalink)  
Old May 27th, 2008, 12:05 PM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 331
Thanks: 23
Thanked 26 Times in 26 Posts
Blog Entries: 1
Rep Power: 1
richyrich is on a distinguished road

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

J

How would you then create a dropdown that, for example, used a primary key field as the option value and firstname & " " & surname as the text?

Just to give me some pointers...

Thanks
Reply With Quote
  #5 (permalink)  
Old May 27th, 2008, 12:08 PM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 537
Thanks: 14
Thanked 40 Times in 39 Posts
Blog Entries: 2
Rep Power: 1
jmurrayhead will become famous soon enough

Awards Showcase
Microsoft Windows Microsoft .Net Microsoft SQL Server Classic ASP 
Total Awards: 4

Default

Because you're using the object as a datasource, you would then set those values to the equivalent properties of the class. You might want to make an additional property "FullName" that returns the concatenated values of FirstName and Surname and set that as the text.
Reply With Quote
The Following User Says Thank You to jmurrayhead For This Useful Post:
richyrich (May 27th, 2008)
  #6 (permalink)  
Old May 27th, 2008, 01:39 PM
Wolffy's Avatar
Slaprentice of Wolves


 
Join Date: Mar 2008
Location: Peoria, IL
Posts: 149
Thanks: 1
Thanked 23 Times in 20 Posts
Rep Power: 1
Wolffy is on a distinguished road

Awards Showcase
Microsoft .Net 
Total Awards: 1

Default

RR;
There are no less than 8 ways to bind a DDL (so I've read). If you are wanting to bind to the results of a query in a data table then:
Code:
Dim sql As String = "SELECT id, firstname || ' ' || surname as fullName from aTable"
Dim da As New SqlDataAdapter(sql, "data source=.;initial catalog=myDB;user id=sa")
 Dim dt As New DataTable
 da.Fill(dt)
 da.Dispose()
 Me.DropDownList1.DataSource = dt
 Me.DropDownList1.DataTextField = "fullName"
 Me.DropDownList1.DataValueField = "id"
 Me.DropDownList1.DataBind() 

Note that || is the concat for Oracle, your SQL may differ.
__________________
Wolffy
------------------------
Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Rework for your specific environment may be required. Void where prohibited by law. Not valid in California. Your mileage may vary.
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
Dynamic dropdown list with multiple records Rebelle ASP Development 4 April 30th, 2008 05:33 AM
Using Dropdown List Value in Database SQL Query richyrich Classic ASP 0 April 2nd, 2008 10:05 AM
Creating Dynamic Dropdown Menu!? jarvelous ASP Development 1 March 20th, 2008 12:30 PM


Sponsored Links

ASP.NET Resource Index
a directory of ASP.NET tutorials, applications, scripts, assemblies and articles for the novice to professional developer.

Free Web Directory
Including Chats and Forums Resources, Offer automatic, instant and free directory submissions.
URLZ Web Directory
URLZ Web Directory

Free Web Directory - Add Your Link
The Little Web Directory
Free Web Directory
Pegasus free web directory is a free directory organised by categories.

Web Directory & SEO Services
dirroot web directory


All times are GMT -4. The time now is 09:20 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
Copyright © 2008 DeveloperBarn.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46