Help, DropDown Web Control not Retuning "SelectedValue"

Discussion in 'ASP .Net Web Controls' started by Paul D. Fox, Jun 1, 2005.

  1. Paul D. Fox

    Paul D. Fox Guest

    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
     
    Paul D. Fox, Jun 1, 2005
    #1
    1. Advertising

  2. 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
    >
    >
    >
     
    Visar Gashi, MCP, Jun 1, 2005
    #2
    1. Advertising

  3. Paul D. Fox

    Paul D. Fox Guest

    Ah yes, I forgot about that.

    Thanks

    "Visar Gashi, MCP" <Visar Gashi, > wrote in
    message news:...
    > 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
    >>
    >>
    >>
     
    Paul D. Fox, Jun 1, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    set selectedvalue on dropdown in datagrid

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Apr 15, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,057
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Apr 15, 2005
  2. Pablo
    Replies:
    0
    Views:
    517
    Pablo
    Apr 6, 2006
  3. Replies:
    5
    Views:
    1,975
    Yamster
    Jun 29, 2006
  4. Replies:
    0
    Views:
    964
  5. jeremy
    Replies:
    0
    Views:
    188
    jeremy
    May 10, 2004
Loading...

Share This Page