Help, DropDown Web Control not Retuning "SelectedValue"

P

Paul D. Fox

I'm trying to utilize a DropDownList as a User Control however, I when I try
to obtain the "SelectedValue" in my .aspx page, it returns a blank value.
Am I doing something wrong here?

This is from my .ascx Page:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

Bind_ddlCategoryList()

End Sub

'Bind ddl_CategoryList
Private Sub Bind_ddlCategoryList()
'Create Database Connection
Dim conPlumPortalApps As SqlConnection
Dim cmdSQL As SqlCommand
Dim drCategories As SqlDataReader

conPlumPortalApps = New
SqlConnection(ConfigurationSettings.AppSettings("plum_PortalApps_ConnectionString"))
cmdSQL = New SqlCommand("SELECT Training_Category_ID,
Training_Category FROM Training_Categories ORDER BY Training_Category",
conPlumPortalApps)

Try
conPlumPortalApps.Open()
drCategories = cmdSQL.ExecuteReader()

ddlCategoryList.DataSource = drCategories
ddlCategoryList.DataValueField = "Training_Category_ID"
ddlCategoryList.DataTextField = "Training_Category"

ddlCategoryList.DataBind()

Dim FirstListItem As New ListItem
FirstListItem.Text = ""
FirstListItem.Value = ""

'Add a blank item at the first position of DropDown.
ddlCategoryList.Items.Insert(0, FirstListItem)

Catch ex As Exception
Response.Write(ex.Message)
Finally
conPlumPortalApps.Dispose()
End Try
End Sub

Public ReadOnly Property CategoryID() As String
Get
_CategoryID = ddlCategoryList.SelectedValue
Return _CategoryID
End Get
'Set(ByVal Value As String)

'End Set
End Property
 
V

Visar Gashi, MCP

Hello Paul,

You are loading the contents of the control every time the page loads, that
is why you have no selection. Make sure you check for postback:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If not Page.IsPostback then
Bind_ddlCategoryList()
End If

End Sub

' bind here
end if

Regards,
Visar Gashi, MCP
 
P

Paul D. Fox

Ah yes, I forgot about that.

Thanks

Visar Gashi said:
Hello Paul,

You are loading the contents of the control every time the page loads,
that
is why you have no selection. Make sure you check for postback:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If not Page.IsPostback then
Bind_ddlCategoryList()
End If

End Sub

' bind here
end if

Regards,
Visar Gashi, MCP
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top