Binding drop down lists in asp.net

Soldato
Joined
18 Oct 2002
Posts
9,158
Hi, I'm having a play around with asp.net at the mo and in particular DB functionality.

I have however encountered a small issue, I am trying to bind a datasource to a drop down list, but all I am getting is the following:

http://augustus.aces.shu.ac.uk/students/jmbradl3/Default.aspx (check out the drop down list there)

Anyone any ideas what could be going on here?

My code is as follows:

Code:
[size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] employeeAdapter [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] BBBDBTableAdapters.EmployeeTableAdapter

ddlBranch.DataSource = employeeAdapter.GetAllEmployeeData

ddlBranch.DataBind()

[/size]

Thanks in advance,

James
 
try adding a DataValueField. If its an employee table so it would probably be the EmployeeID.

Code:
ddlBranch.DataValueField = "EmployeeID"
 
That works great, thanks.

What I've done is display the name in the drop down list, how would I go about getting the ID of the employee selected in the drop down box?

Thanks for the help,

Regards,

James
 
I think the way I would do it is make a database query and use the contents of the drop down list as a parameter (in your case ddlBranch.SelectedItem.Value) then you could return the results including the ID (or just the ID, up to you) on the page.
 
You will find that the DropDownList has a property called SelectedValue. Although it returns a string, it'll actually be a string representing the Id of the employee. Just Int32.Parse(...) it to get back the id.

For this to work properly, you'll need to set DataValueField to the name of the id column and DataTextField to the name of the employee name column.
 
That sounds ideal, so would I need something like the followig?


Code:
[size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] myInt [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Integer

[/color][/size][size=2]myInt = Int32.Parse(DropDownList1.SelectedValue)

[/size]
 
Good stuff, I've tried that, check out the website link at the top.


Slight error however, whenever I click the button, which does the command outlined above, I always seem to get the ID of the first name in the box, not whichever one is selected.

Any ideas as to what's going on here.?

Cheers for the help,

James
 
Just a stab in the dark but... that may be happening because the code that is setting the intial selected value is executing on post back as well as first time the page is loaded.

Try putting an "if (!IsPostBack) {}" around the code which sets the initial selection.
 
One more quick question; I have a grid control and want to customise the headings and the order by which the columns are displays, any ideas on how I' go about doing this?

Regards,

James
 
This is easy. Go to design view and select your grid. Click the small little down arrow at the top right of the selection rectangle. Then select configure columns or something like that.

You need to add to the bottom list the columns you actually want to display and their ordering. If you highlight a column in the bottom list, you can set a field called header name or something which will set the displayed column name.
 
Back
Top Bottom