Home All Groups Group Topic Archive Search About

Help, DropDown Web Control not Retuning "SelectedValue"

Author
1 Jun 2005 8:30 PM
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

Author
1 Jun 2005 9:07 PM
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

Show quoteHide quote
"Paul D. Fox" wrote:

> 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
>
>
>
Author
1 Jun 2005 9:10 PM
Paul D. Fox
Ah yes, I forgot about that.

Thanks

Show quoteHide quote
"Visar Gashi, MCP" <Visar Gashi, M**@discussions.microsoft.com> wrote in
message news:6F33EAE9-FF7A-4F17-A561-5A7ECEBD4E41@microsoft.com...
> 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
>
> "Paul D. Fox" wrote:
>
>> 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
>>
>>
>>