Populate DropDown in Master Page

D

David C

I have a web application with a DropDownList at the top of the Master Page
that I want to populate via code rather than SqlDataSource. I am doing the
same process successfully in other pages in places like RowDatabound event
as I only need DropDowns in edit mode. I cannot get it to work on my
Page_Load event on my Master Page. Can someone tell me what I am doing
wrong? I want to get rid of the SqlDataSource control link to the
dropdownlist. Thanks.

Below is the code-behind on the Master Page and below that is the Class
function I am calling. I tested the SELECT from the database and got 1
record back successfully and I also checked the value in intStaffID and it
is fine.

If Not Request.Cookies("mystaffid") Is Nothing Then
Dim intStaffID As Integer =
Convert.ToInt32(Request.Cookies("mystaffid").Value)
Dim ddl As DropDownList =
Page.Master.FindControl("ddlProgramCode")
ddl.DataSource = LookupClass.GetStaffPrograms(intStaffID)
ddl.DataValueField = "ProgramID"
ddl.DataTextField = "ProgramName"
ddl.DataBind()
ddl.Items.Insert(0, New ListItem("All My Programs", "0"))
End If


Public Shared Function GetStaffPrograms(ByVal intStaffID As Int32) As
SqlDataReader
Dim conData As SqlConnection = New
SqlConnection(ConfigurationManager.ConnectionStrings("MCConnectionString").ConnectionString)
conData.Open()

Dim strSQL As String

strSQL = "SELECT [ProgramID], [ProgramName]" & _
" FROM [vw_StaffPrograms]" & _
" WHERE [StaffID] = " & intStaffID.ToString & _
" ORDER BY [ProgramName]"

Dim cmdSel As SqlCommand = New SqlCommand(strSQL, conData)
Dim dtr As SqlDataReader = cmdSel.ExecuteReader()
Return dtr
End Function
 
D

David C

When I debugged it and traced through the code it was telling me that the
function was not returning any values. Yet when I run the query on the
database, I get 1 record back.

David
 
D

David C

Yes and Yes. I have the exact same code in a RowDatabound event on this
same FormView and that is working correct. That is why I wondered if it had
something to do with being in the Page_Load event, but it is really
confusing...even when debugging. Not sure what else to do except I know for
a fact that the previous process using a SqlDatasource worked perfectly but
I don't want to do that.

David
 
D

David C

I wish asp.net and VS 2008 had an Immediate Window so I could run just the
function.

David
 
D

David C

Mark Rae said:
Well obviously not!

Run the app, put a breakpoint somewhere, and *THEN* run the function in
the immediate window...
Mark,
I think the problem is that during a Master Page_Load event the values I am
expecting are not yet available (and not sure why). I put them into visible
fields on the content page and the values did not show up until I did a
postback. Would this make a difference?

David
 
H

Hillbilly

<snip />

For a DDL why not just move the code to Page_PreRender in your base class?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top