Register Blogs FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

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

Sponsored Links

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
 
LinkBack Thread Tools Display Modes
  #1  
Old May 27th, 2008, 06:51 AM
Shem's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 263
Thanks: 31
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  
Old May 27th, 2008, 07:18 AM
richyrich's Avatar
Super Moderator
 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 454
Thanks: 31
Thanked 42 Times in 42 Posts
Blog Entries: 1
Rep Power: 2
richyrich will become famous soon enoughrichyrich will become famous soon enough

Awards Showcase
JavaScript Classic ASP 
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  
Old May 27th, 2008, 07:29 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 941
Thanks: 22
Thanked 93 Times in 90 Posts
Blog Entries: 5
Rep Power: 4
jmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the rough

Awards Showcase
Microsoft Windows Microsoft SQL Server Microsoft .Net 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
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

If you like it here...throw us a few bones to help
support us.

Join our Folding team: DeveloperBarn Folding

Reply With Quote
  #4  
Old May 27th, 2008, 12:05 PM
richyrich's Avatar
Super Moderator
 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 454
Thanks: 31
Thanked 42 Times in 42 Posts
Blog Entries: 1
Rep Power: 2
richyrich will become famous soon enoughrichyrich will become famous soon enough

Awards Showcase
JavaScript Classic ASP 
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  
Old May 27th, 2008, 12:08 PM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 941
Thanks: 22
Thanked 93 Times in 90 Posts
Blog Entries: 5
Rep Power: 4
jmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the rough

Awards Showcase
Microsoft Windows Microsoft SQL Server Microsoft .Net 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  
Old May 27th, 2008, 01:39 PM
Wolffy's Avatar
Slaprentice of Wolves
 
Join Date: Mar 2008
Location: Peoria, IL
Posts: 175
Thanks: 3
Thanked 24 Times in 21 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 ASP Code Samples 0 April 2nd, 2008 10:05 AM
Creating Dynamic Dropdown Menu!? jarvelous ASP Development 1 March 20th, 2008 12:30 PM


All times are GMT -4. The time now is 12:39 AM.



Content Relevant URLs by vBSEO 3.2.0