|
-
Sep 14th, 2005, 04:45 AM
#1
Thread Starter
Fanatic Member
DB Class - Populate DropdownList
Hi,
I'm trying to write a DB actions class for my web project to help reduce code duplication for example I want to show a dropdownlist with countries, populated from my DB, on more than one page, so instead of writing the code in each required page I thought that if I put it in a class I could then call a function i.e. populateCountriesDDL when required.
Does anyone know how to do this? Am I going about this the correct way or is there a better solution, with classic asp I would create an asp include file and add it in wherever needed
I've tried referencing
VB Code:
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
but get error message - namespace could not be found for each of the above
Thought I could pass the function the DDL and populate it from there?
Any help will be greatly appreciated
Cheers Al
-
Sep 14th, 2005, 05:09 AM
#2
Fanatic Member
Re: DB Class - Populate DropdownList
i havent a clue this is how i would go about it, IM NEW
perhaps some experinced members could read this and help further.
i havent done this yet but this is how im going to try it
create a class and do a get get countries function as dataset
public shared function getcountries() as dataset
dim dscountries as new dataset
dim sselect as string
' your select statement'
dim dacountries as new sqldataadapter(sSelect, connection())
dacountries.fill(dscountries, "country table")
'connection() is a private funtion in the same class that connects to database
then in your form code behind file - bind the ddl
this prob didnt help but might get the ball rolling for you
Last edited by d2005; Sep 14th, 2005 at 05:13 AM.
it works 60% of the time, all the time.
-
Sep 14th, 2005, 05:36 AM
#3
Re: DB Class - Populate DropdownList
A dropdownlist can be bound to an array. So what you need in your class is a function which will return an array containing all the countries with their values in the array. Make sense?
-
Sep 14th, 2005, 06:03 AM
#4
Thread Starter
Fanatic Member
Re: DB Class - Populate DropdownList
D2005,
Thanks, it's not quite what I had in mind, but as you say it's a start
Cheers Al
-
Sep 14th, 2005, 06:27 AM
#5
Hyperactive Member
Re: DB Class - Populate DropdownList
Create a class with a static function that accepts a DropDownList as a parameter, then populates it.
To call it use;
MyClass.PopulateCountries(this.DropDownCountries);
-
Sep 14th, 2005, 06:47 AM
#6
Thread Starter
Fanatic Member
Re: DB Class - Populate DropdownList
Mendhak,
Thanks for that, I did think of doing it that way but would mean on each page I have to loop through the array populating my DDL as I want to be able to set the default selected item i.e. for when a user wants to edit their profile etc. I'm told you can't set the selected item by
VB Code:
ddlCountry.SelectedIndex = "United Kingdom"
Instead I'm doing it like this as I don't know what the index is. If there is a better/more efficient way of doing this please let me know
VB Code:
dr = myCmd.ExecuteReader()
i = 0
ddlCountry.Items.Add("[Select]")
ddlCountry.Items(i).Value = "0"
Do While dr.Read()
i += 1
ddlCountry.Items.Add(Trim(CStr(dr("Country"))))
ddlCountry.Items(i).Value = Trim(CStr(dr("Country_Code")))
If sdefault = Trim(CStr(dr("Country"))) Then
ddlCountry.SelectedIndex = i
End If
Loop
What I wanted to do was pass in a ref to a DDL and a default item and the function would take care of the rest for me.
VB Code:
MyClass.PopulateCountries(this.DropDownCountries, sDefault)
As suggested by GlenW however I can't seem to reference
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
when I try to create my populateCountries function there is no dropdownlist in the list of classes (see bold below) -
VB Code:
Public Function PopulateCountries(byRef myDDL as [b]DropDownList[/b], optional byRef sDefault as string = "0") as boolean
I'm sure this can be done, but being a newbie I'm just doing it incorrectly, I truely welcome all guidence
Hopefully the above makes sense
If not let me know
Cheers Al
-
Sep 14th, 2005, 02:43 PM
#7
Hyperactive Member
Re: DB Class - Populate DropdownList
Have you referenced System.Web?
If you are writing a class utility that is not part of an asp.net project it won't be referenced automatically.
Just right click on References in solution explorer and then click Add Reference.
-
Sep 16th, 2005, 08:46 AM
#8
Thread Starter
Fanatic Member
Re: DB Class - Populate DropdownList
Glen,
Yes I'm already referencing System.Web, but I can't find these bad boys -
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
I have an asp.net web project and I've added a class project to it.
-
Sep 16th, 2005, 09:04 AM
#9
Hyperactive Member
Re: DB Class - Populate DropdownList
 Originally Posted by aconybeare
I have an asp.net web project and I've added a class project to it.
Have you added the System.Web reference to the new class project?
-
Sep 16th, 2005, 10:18 AM
#10
Thread Starter
Fanatic Member
Re: DB Class - Populate DropdownList
Glen,
I was just about to reply yes, when I thought I'd better check and it seems not. I've now added it in and the others are now showing okay, yet another school boy error!
Thanks for your help, hopefully I'm on my way now
Cheers Al
-
Sep 16th, 2005, 10:28 AM
#11
Hyperactive Member
Re: DB Class - Populate DropdownList
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|