returning the selected value of a listbox control inside a user control - please urgent!

Discussion in 'ASP .Net' started by Moe Sizlak, Jul 20, 2004.

  1. Moe Sizlak

    Moe Sizlak Guest

    Hi There,

    I am trying to return the value of a listbox control that is included as a
    user control, I can return the name of the control but I can't access the
    integer value of the selected item, what do I need to do in order to return
    the "option value" of the control?

    Moe

    !--- returned value of the control

    _ctl1_lstCategorie


    !-- compiled page code

    <select name="_ctl1:lstCategorie" size="1"
    onchange="__doPostBack('_ctl1$lstCategorie','')" language="javascript"
    id="_ctl1_lstCategorie">
    <option value="0">-- Choose a Category --</option>
    <option value="6">...Control Valves
    ....</option>
    <option value="10">...Dripline
    ....</option>
    </select>



    !--- page code
    <%@ Control Language="vb" EnableViewState="true" %>

    <% @Import Namespace="System.Data" %>
    <% @Import Namespace="System.Data.SqlClient" %>

    Public Sub Page_Load(sender as Object, e as EventArgs)

    Public ReadOnly Property SelectedCategoriesID As String
    Get
    return lstCategorie.ClientID
    End Get
    End Property

    If Not Page.IsPostBack Then
    BindStates()
    LoadCategories()


    response.write (lstCategorie.ClientID)

    end if
    End Sub

    Public Sub LoadCategories()
    Try
    Dim myConnection as New
    SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
    Const strSQL as String = "SELECT CategoryID, '...' + CategoryDesc + '...'
    AS CategoryDesc " & _
    "FROM tblProductCategories ORDER BY CategoryDesc"
    Dim myCommand as New SqlCommand(strSQL, myConnection)
    myConnection.Open()
    Dim objDR as SqlDataReader
    objDR = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    lstCategorie.DataSource = objDR
    lstCategorie.DataBind()
    lstCategorie.Items.Insert(0, new ListItem("-- Choose a Category --","0"))

    Catch ex as InvalidCastException
    Status.Text = ex.ToString()

    Catch ex As SqlException
    Status.Text = "Database error: " & ex.message

    Catch ex As Exception
    Status.Text = "General error: " & ex.message
    End Try

    End Sub

    !---page code
    <table width="100%" border="0" cellspacing="0" cellpadding="5" height="20">

    <td height="20" width="11%">
    <div align="right"><font face="Verdana, Arial, Helvetica, sans-serif"
    size="1" color="#FFFFFF">Browse
    :</font></div>
    </td>
    <td height="20" width="14%">

    <div align="left">
    <asp:listbox id="lstCategorie" runat="server" Rows="1"
    AutoPostBack="true"
    DataTextField="CategoryDesc" DataValueField="CategoryID" />

    </div>

    </td>
     
    Moe Sizlak, Jul 20, 2004
    #1
    1. Advertising

  2. Moe Sizlak

    Mike Smith Guest

    list controls have a SelectedText and a SelectedValue property... u can use
    both actually

    ListName.SelectedText.Value
    or
    ListName.SelectedValue

    SelectedText.Text gives u the selected text on the listbox

    --
    Regards,
    Mike
    http://bikesh.europe.webmatrixhosting.net
    http://www.planetsourcecode.com (search keyword: phoetus)

    "Moe Sizlak" <> wrote in message
    news:%...
    > Hi There,
    >
    > I am trying to return the value of a listbox control that is included as a
    > user control, I can return the name of the control but I can't access the
    > integer value of the selected item, what do I need to do in order to

    return
    > the "option value" of the control?
    >
    > Moe
    >
    > !--- returned value of the control
    >
    > _ctl1_lstCategorie
    >
    >
    > !-- compiled page code
    >
    > <select name="_ctl1:lstCategorie" size="1"
    > onchange="__doPostBack('_ctl1$lstCategorie','')" language="javascript"
    > id="_ctl1_lstCategorie">
    > <option value="0">-- Choose a Category --</option>
    > <option value="6">...Control Valves
    > ...</option>
    > <option value="10">...Dripline
    > ...</option>
    > </select>
    >
    >
    >
    > !--- page code
    > <%@ Control Language="vb" EnableViewState="true" %>
    >
    > <% @Import Namespace="System.Data" %>
    > <% @Import Namespace="System.Data.SqlClient" %>
    >
    > Public Sub Page_Load(sender as Object, e as EventArgs)
    >
    > Public ReadOnly Property SelectedCategoriesID As String
    > Get
    > return lstCategorie.ClientID
    > End Get
    > End Property
    >
    > If Not Page.IsPostBack Then
    > BindStates()
    > LoadCategories()
    >
    >
    > response.write (lstCategorie.ClientID)
    >
    > end if
    > End Sub
    >
    > Public Sub LoadCategories()
    > Try
    > Dim myConnection as New
    > SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
    > Const strSQL as String = "SELECT CategoryID, '...' + CategoryDesc +

    '...'
    > AS CategoryDesc " & _
    > "FROM tblProductCategories ORDER BY CategoryDesc"
    > Dim myCommand as New SqlCommand(strSQL, myConnection)
    > myConnection.Open()
    > Dim objDR as SqlDataReader
    > objDR = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    > lstCategorie.DataSource = objDR
    > lstCategorie.DataBind()
    > lstCategorie.Items.Insert(0, new ListItem("-- Choose a

    Category --","0"))
    >
    > Catch ex as InvalidCastException
    > Status.Text = ex.ToString()
    >
    > Catch ex As SqlException
    > Status.Text = "Database error: " & ex.message
    >
    > Catch ex As Exception
    > Status.Text = "General error: " & ex.message
    > End Try
    >
    > End Sub
    >
    > !---page code
    > <table width="100%" border="0" cellspacing="0" cellpadding="5"

    height="20">
    >
    > <td height="20" width="11%">
    > <div align="right"><font face="Verdana, Arial, Helvetica,

    sans-serif"
    > size="1" color="#FFFFFF">Browse
    > :</font></div>
    > </td>
    > <td height="20" width="14%">
    >
    > <div align="left">
    > <asp:listbox id="lstCategorie" runat="server" Rows="1"
    > AutoPostBack="true"
    > DataTextField="CategoryDesc" DataValueField="CategoryID" />
    >
    > </div>
    >
    > </td>
    >
    >

    list controls have a SelectedText and a SelectedValue property... u can use
    both actually

    ListName.SelectedText.Value
    or
    ListName.SelectedValue

    SelectedText.Text gives u the selected text on the listbox

    --
    Regards,
    Mike
    http://bikesh.europe.webmatrixhosting.net
    http://www.planetsourcecode.com (search keyword: phoetus)

    "Moe Sizlak" <> wrote in message
    news:%...
    > Hi There,
    >
    > I am trying to return the value of a listbox control that is included as a
    > user control, I can return the name of the control but I can't access the
    > integer value of the selected item, what do I need to do in order to

    return
    > the "option value" of the control?
    >
    > Moe
    >
    > !--- returned value of the control
    >
    > _ctl1_lstCategorie
    >
    >
    > !-- compiled page code
    >
    > <select name="_ctl1:lstCategorie" size="1"
    > onchange="__doPostBack('_ctl1$lstCategorie','')" language="javascript"
    > id="_ctl1_lstCategorie">
    > <option value="0">-- Choose a Category --</option>
    > <option value="6">...Control Valves
    > ...</option>
    > <option value="10">...Dripline
    > ...</option>
    > </select>
    >
    >
    >
    > !--- page code
    > <%@ Control Language="vb" EnableViewState="true" %>
    >
    > <% @Import Namespace="System.Data" %>
    > <% @Import Namespace="System.Data.SqlClient" %>
    >
    > Public Sub Page_Load(sender as Object, e as EventArgs)
    >
    > Public ReadOnly Property SelectedCategoriesID As String
    > Get
    > return lstCategorie.ClientID
    > End Get
    > End Property
    >
    > If Not Page.IsPostBack Then
    > BindStates()
    > LoadCategories()
    >
    >
    > response.write (lstCategorie.ClientID)
    >
    > end if
    > End Sub
    >
    > Public Sub LoadCategories()
    > Try
    > Dim myConnection as New
    > SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
    > Const strSQL as String = "SELECT CategoryID, '...' + CategoryDesc +

    '...'
    > AS CategoryDesc " & _
    > "FROM tblProductCategories ORDER BY CategoryDesc"
    > Dim myCommand as New SqlCommand(strSQL, myConnection)
    > myConnection.Open()
    > Dim objDR as SqlDataReader
    > objDR = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    > lstCategorie.DataSource = objDR
    > lstCategorie.DataBind()
    > lstCategorie.Items.Insert(0, new ListItem("-- Choose a

    Category --","0"))
    >
    > Catch ex as InvalidCastException
    > Status.Text = ex.ToString()
    >
    > Catch ex As SqlException
    > Status.Text = "Database error: " & ex.message
    >
    > Catch ex As Exception
    > Status.Text = "General error: " & ex.message
    > End Try
    >
    > End Sub
    >
    > !---page code
    > <table width="100%" border="0" cellspacing="0" cellpadding="5"

    height="20">
    >
    > <td height="20" width="11%">
    > <div align="right"><font face="Verdana, Arial, Helvetica,

    sans-serif"
    > size="1" color="#FFFFFF">Browse
    > :</font></div>
    > </td>
    > <td height="20" width="14%">
    >
    > <div align="left">
    > <asp:listbox id="lstCategorie" runat="server" Rows="1"
    > AutoPostBack="true"
    > DataTextField="CategoryDesc" DataValueField="CategoryID" />
    >
    > </div>
    >
    > </td>
    >
    >
     
    Mike Smith, Jul 20, 2004
    #2
    1. Advertising

  3. Moe Sizlak

    Moe Sizlak Guest

    Hi Mike,

    I have tried this and it returns 2 different results:
    for
    lstCategorie.SelectedText.Value
    BC30456: 'SelectedText' is not a member of
    'System.Web.UI.WebControls.ListBox'.

    for
    lstCategorie.SelectedValue
    I get nothing at all from the control

    Any other ideas?

    Moe



    "Mike Smith" <> wrote in message
    news:#...
    > list controls have a SelectedText and a SelectedValue property... u can

    use
    > both actually
    >
    > ListName.SelectedText.Value
    > or
    > ListName.SelectedValue
    >
    > SelectedText.Text gives u the selected text on the listbox
    >
    > --
    > Regards,
    > Mike
    > http://bikesh.europe.webmatrixhosting.net
    > http://www.planetsourcecode.com (search keyword: phoetus)
    >
    > "Moe Sizlak" <> wrote in message
    > news:%...
    > > Hi There,
    > >
    > > I am trying to return the value of a listbox control that is included as

    a
    > > user control, I can return the name of the control but I can't access

    the
    > > integer value of the selected item, what do I need to do in order to

    > return
    > > the "option value" of the control?
    > >
    > > Moe
    > >
    > > !--- returned value of the control
    > >
    > > _ctl1_lstCategorie
    > >
    > >
    > > !-- compiled page code
    > >
    > > <select name="_ctl1:lstCategorie" size="1"
    > > onchange="__doPostBack('_ctl1$lstCategorie','')" language="javascript"
    > > id="_ctl1_lstCategorie">
    > > <option value="0">-- Choose a Category --</option>
    > > <option value="6">...Control Valves
    > > ...</option>
    > > <option value="10">...Dripline
    > > ...</option>
    > > </select>
    > >
    > >
    > >
    > > !--- page code
    > > <%@ Control Language="vb" EnableViewState="true" %>
    > >
    > > <% @Import Namespace="System.Data" %>
    > > <% @Import Namespace="System.Data.SqlClient" %>
    > >
    > > Public Sub Page_Load(sender as Object, e as EventArgs)
    > >
    > > Public ReadOnly Property SelectedCategoriesID As String
    > > Get
    > > return lstCategorie.ClientID
    > > End Get
    > > End Property
    > >
    > > If Not Page.IsPostBack Then
    > > BindStates()
    > > LoadCategories()
    > >
    > >
    > > response.write (lstCategorie.ClientID)
    > >
    > > end if
    > > End Sub
    > >
    > > Public Sub LoadCategories()
    > > Try
    > > Dim myConnection as New
    > > SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
    > > Const strSQL as String = "SELECT CategoryID, '...' + CategoryDesc +

    > '...'
    > > AS CategoryDesc " & _
    > > "FROM tblProductCategories ORDER BY CategoryDesc"
    > > Dim myCommand as New SqlCommand(strSQL, myConnection)
    > > myConnection.Open()
    > > Dim objDR as SqlDataReader
    > > objDR = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    > > lstCategorie.DataSource = objDR
    > > lstCategorie.DataBind()
    > > lstCategorie.Items.Insert(0, new ListItem("-- Choose a

    > Category --","0"))
    > >
    > > Catch ex as InvalidCastException
    > > Status.Text = ex.ToString()
    > >
    > > Catch ex As SqlException
    > > Status.Text = "Database error: " & ex.message
    > >
    > > Catch ex As Exception
    > > Status.Text = "General error: " & ex.message
    > > End Try
    > >
    > > End Sub
    > >
    > > !---page code
    > > <table width="100%" border="0" cellspacing="0" cellpadding="5"

    > height="20">
    > >
    > > <td height="20" width="11%">
    > > <div align="right"><font face="Verdana, Arial, Helvetica,

    > sans-serif"
    > > size="1" color="#FFFFFF">Browse
    > > :</font></div>
    > > </td>
    > > <td height="20" width="14%">
    > >
    > > <div align="left">
    > > <asp:listbox id="lstCategorie" runat="server" Rows="1"
    > > AutoPostBack="true"
    > > DataTextField="CategoryDesc" DataValueField="CategoryID"

    />
    > >
    > > </div>
    > >
    > > </td>
    > >
    > >

    > list controls have a SelectedText and a SelectedValue property... u can

    use
    > both actually
    >
    > ListName.SelectedText.Value
    > or
    > ListName.SelectedValue
    >
    > SelectedText.Text gives u the selected text on the listbox
    >
    > --
    > Regards,
    > Mike
    > http://bikesh.europe.webmatrixhosting.net
    > http://www.planetsourcecode.com (search keyword: phoetus)
    >
    > "Moe Sizlak" <> wrote in message
    > news:%...
    > > Hi There,
    > >
    > > I am trying to return the value of a listbox control that is included as

    a
    > > user control, I can return the name of the control but I can't access

    the
    > > integer value of the selected item, what do I need to do in order to

    > return
    > > the "option value" of the control?
    > >
    > > Moe
    > >
    > > !--- returned value of the control
    > >
    > > _ctl1_lstCategorie
    > >
    > >
    > > !-- compiled page code
    > >
    > > <select name="_ctl1:lstCategorie" size="1"
    > > onchange="__doPostBack('_ctl1$lstCategorie','')" language="javascript"
    > > id="_ctl1_lstCategorie">
    > > <option value="0">-- Choose a Category --</option>
    > > <option value="6">...Control Valves
    > > ...</option>
    > > <option value="10">...Dripline
    > > ...</option>
    > > </select>
    > >
    > >
    > >
    > > !--- page code
    > > <%@ Control Language="vb" EnableViewState="true" %>
    > >
    > > <% @Import Namespace="System.Data" %>
    > > <% @Import Namespace="System.Data.SqlClient" %>
    > >
    > > Public Sub Page_Load(sender as Object, e as EventArgs)
    > >
    > > Public ReadOnly Property SelectedCategoriesID As String
    > > Get
    > > return lstCategorie.ClientID
    > > End Get
    > > End Property
    > >
    > > If Not Page.IsPostBack Then
    > > BindStates()
    > > LoadCategories()
    > >
    > >
    > > response.write (lstCategorie.ClientID)
    > >
    > > end if
    > > End Sub
    > >
    > > Public Sub LoadCategories()
    > > Try
    > > Dim myConnection as New
    > > SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
    > > Const strSQL as String = "SELECT CategoryID, '...' + CategoryDesc +

    > '...'
    > > AS CategoryDesc " & _
    > > "FROM tblProductCategories ORDER BY CategoryDesc"
    > > Dim myCommand as New SqlCommand(strSQL, myConnection)
    > > myConnection.Open()
    > > Dim objDR as SqlDataReader
    > > objDR = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    > > lstCategorie.DataSource = objDR
    > > lstCategorie.DataBind()
    > > lstCategorie.Items.Insert(0, new ListItem("-- Choose a

    > Category --","0"))
    > >
    > > Catch ex as InvalidCastException
    > > Status.Text = ex.ToString()
    > >
    > > Catch ex As SqlException
    > > Status.Text = "Database error: " & ex.message
    > >
    > > Catch ex As Exception
    > > Status.Text = "General error: " & ex.message
    > > End Try
    > >
    > > End Sub
    > >
    > > !---page code
    > > <table width="100%" border="0" cellspacing="0" cellpadding="5"

    > height="20">
    > >
    > > <td height="20" width="11%">
    > > <div align="right"><font face="Verdana, Arial, Helvetica,

    > sans-serif"
    > > size="1" color="#FFFFFF">Browse
    > > :</font></div>
    > > </td>
    > > <td height="20" width="14%">
    > >
    > > <div align="left">
    > > <asp:listbox id="lstCategorie" runat="server" Rows="1"
    > > AutoPostBack="true"
    > > DataTextField="CategoryDesc" DataValueField="CategoryID"

    />
    > >
    > > </div>
    > >
    > > </td>
    > >
    > >

    >
    >
     
    Moe Sizlak, Jul 20, 2004
    #3
  4. Moe Sizlak

    Mike Smith Guest

    geez sorry i must have dozed off !
    its SelectedItem.Text and SelectedItem.Value
    there is no SelectText.. i merged it myself.. hahahahaha.. sorry..

    Response.Write(ListBox1.SelectedItem.Value)

    Response.Write(ListBox1.SelectedItem.Text)

    Response.Write(ListBox1.SelectedValue.ToString)


    --
    Regards,
    Mike
    http://bikesh.europe.webmatrixhosting.net
    http://www.planetsourcecode.com (search keyword: phoetus)

    "Moe Sizlak" <> wrote in message
    news:...
    > Hi Mike,
    >
    > I have tried this and it returns 2 different results:
    > for
    > lstCategorie.SelectedText.Value
    > BC30456: 'SelectedText' is not a member of
    > 'System.Web.UI.WebControls.ListBox'.
    >
    > for
    > lstCategorie.SelectedValue
    > I get nothing at all from the control
    >
    > Any other ideas?
    >
    > Moe
    >
    >
    >
    > "Mike Smith" <> wrote in message
    > news:#...
    > > list controls have a SelectedText and a SelectedValue property... u can

    > use
    > > both actually
    > >
    > > ListName.SelectedText.Value
    > > or
    > > ListName.SelectedValue
    > >
    > > SelectedText.Text gives u the selected text on the listbox
    > >
    > > --
    > > Regards,
    > > Mike
    > > http://bikesh.europe.webmatrixhosting.net
    > > http://www.planetsourcecode.com (search keyword: phoetus)
    > >
    > > "Moe Sizlak" <> wrote in message
    > > news:%...
    > > > Hi There,
    > > >
    > > > I am trying to return the value of a listbox control that is included

    as
    > a
    > > > user control, I can return the name of the control but I can't access

    > the
    > > > integer value of the selected item, what do I need to do in order to

    > > return
    > > > the "option value" of the control?
    > > >
    > > > Moe
    > > >
    > > > !--- returned value of the control
    > > >
    > > > _ctl1_lstCategorie
    > > >
    > > >
    > > > !-- compiled page code
    > > >
    > > > <select name="_ctl1:lstCategorie" size="1"
    > > > onchange="__doPostBack('_ctl1$lstCategorie','')" language="javascript"
    > > > id="_ctl1_lstCategorie">
    > > > <option value="0">-- Choose a Category --</option>
    > > > <option value="6">...Control Valves
    > > > ...</option>
    > > > <option value="10">...Dripline
    > > > ...</option>
    > > > </select>
    > > >
    > > >
    > > >
    > > > !--- page code
    > > > <%@ Control Language="vb" EnableViewState="true" %>
    > > >
    > > > <% @Import Namespace="System.Data" %>
    > > > <% @Import Namespace="System.Data.SqlClient" %>
    > > >
    > > > Public Sub Page_Load(sender as Object, e as EventArgs)
    > > >
    > > > Public ReadOnly Property SelectedCategoriesID As String
    > > > Get
    > > > return lstCategorie.ClientID
    > > > End Get
    > > > End Property
    > > >
    > > > If Not Page.IsPostBack Then
    > > > BindStates()
    > > > LoadCategories()
    > > >
    > > >
    > > > response.write (lstCategorie.ClientID)
    > > >
    > > > end if
    > > > End Sub
    > > >
    > > > Public Sub LoadCategories()
    > > > Try
    > > > Dim myConnection as New
    > > > SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
    > > > Const strSQL as String = "SELECT CategoryID, '...' + CategoryDesc +

    > > '...'
    > > > AS CategoryDesc " & _
    > > > "FROM tblProductCategories ORDER BY CategoryDesc"
    > > > Dim myCommand as New SqlCommand(strSQL, myConnection)
    > > > myConnection.Open()
    > > > Dim objDR as SqlDataReader
    > > > objDR = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    > > > lstCategorie.DataSource = objDR
    > > > lstCategorie.DataBind()
    > > > lstCategorie.Items.Insert(0, new ListItem("-- Choose a

    > > Category --","0"))
    > > >
    > > > Catch ex as InvalidCastException
    > > > Status.Text = ex.ToString()
    > > >
    > > > Catch ex As SqlException
    > > > Status.Text = "Database error: " & ex.message
    > > >
    > > > Catch ex As Exception
    > > > Status.Text = "General error: " & ex.message
    > > > End Try
    > > >
    > > > End Sub
    > > >
    > > > !---page code
    > > > <table width="100%" border="0" cellspacing="0" cellpadding="5"

    > > height="20">
    > > >
    > > > <td height="20" width="11%">
    > > > <div align="right"><font face="Verdana, Arial, Helvetica,

    > > sans-serif"
    > > > size="1" color="#FFFFFF">Browse
    > > > :</font></div>
    > > > </td>
    > > > <td height="20" width="14%">
    > > >
    > > > <div align="left">
    > > > <asp:listbox id="lstCategorie" runat="server" Rows="1"
    > > > AutoPostBack="true"
    > > > DataTextField="CategoryDesc"

    DataValueField="CategoryID"
    > />
    > > >
    > > > </div>
    > > >
    > > > </td>
    > > >
    > > >

    > > list controls have a SelectedText and a SelectedValue property... u can

    > use
    > > both actually
    > >
    > > ListName.SelectedText.Value
    > > or
    > > ListName.SelectedValue
    > >
    > > SelectedText.Text gives u the selected text on the listbox
    > >
    > > --
    > > Regards,
    > > Mike
    > > http://bikesh.europe.webmatrixhosting.net
    > > http://www.planetsourcecode.com (search keyword: phoetus)
    > >
    > > "Moe Sizlak" <> wrote in message
    > > news:%...
    > > > Hi There,
    > > >
    > > > I am trying to return the value of a listbox control that is included

    as
    > a
    > > > user control, I can return the name of the control but I can't access

    > the
    > > > integer value of the selected item, what do I need to do in order to

    > > return
    > > > the "option value" of the control?
    > > >
    > > > Moe
    > > >
    > > > !--- returned value of the control
    > > >
    > > > _ctl1_lstCategorie
    > > >
    > > >
    > > > !-- compiled page code
    > > >
    > > > <select name="_ctl1:lstCategorie" size="1"
    > > > onchange="__doPostBack('_ctl1$lstCategorie','')" language="javascript"
    > > > id="_ctl1_lstCategorie">
    > > > <option value="0">-- Choose a Category --</option>
    > > > <option value="6">...Control Valves
    > > > ...</option>
    > > > <option value="10">...Dripline
    > > > ...</option>
    > > > </select>
    > > >
    > > >
    > > >
    > > > !--- page code
    > > > <%@ Control Language="vb" EnableViewState="true" %>
    > > >
    > > > <% @Import Namespace="System.Data" %>
    > > > <% @Import Namespace="System.Data.SqlClient" %>
    > > >
    > > > Public Sub Page_Load(sender as Object, e as EventArgs)
    > > >
    > > > Public ReadOnly Property SelectedCategoriesID As String
    > > > Get
    > > > return lstCategorie.ClientID
    > > > End Get
    > > > End Property
    > > >
    > > > If Not Page.IsPostBack Then
    > > > BindStates()
    > > > LoadCategories()
    > > >
    > > >
    > > > response.write (lstCategorie.ClientID)
    > > >
    > > > end if
    > > > End Sub
    > > >
    > > > Public Sub LoadCategories()
    > > > Try
    > > > Dim myConnection as New
    > > > SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
    > > > Const strSQL as String = "SELECT CategoryID, '...' + CategoryDesc +

    > > '...'
    > > > AS CategoryDesc " & _
    > > > "FROM tblProductCategories ORDER BY CategoryDesc"
    > > > Dim myCommand as New SqlCommand(strSQL, myConnection)
    > > > myConnection.Open()
    > > > Dim objDR as SqlDataReader
    > > > objDR = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    > > > lstCategorie.DataSource = objDR
    > > > lstCategorie.DataBind()
    > > > lstCategorie.Items.Insert(0, new ListItem("-- Choose a

    > > Category --","0"))
    > > >
    > > > Catch ex as InvalidCastException
    > > > Status.Text = ex.ToString()
    > > >
    > > > Catch ex As SqlException
    > > > Status.Text = "Database error: " & ex.message
    > > >
    > > > Catch ex As Exception
    > > > Status.Text = "General error: " & ex.message
    > > > End Try
    > > >
    > > > End Sub
    > > >
    > > > !---page code
    > > > <table width="100%" border="0" cellspacing="0" cellpadding="5"

    > > height="20">
    > > >
    > > > <td height="20" width="11%">
    > > > <div align="right"><font face="Verdana, Arial, Helvetica,

    > > sans-serif"
    > > > size="1" color="#FFFFFF">Browse
    > > > :</font></div>
    > > > </td>
    > > > <td height="20" width="14%">
    > > >
    > > > <div align="left">
    > > > <asp:listbox id="lstCategorie" runat="server" Rows="1"
    > > > AutoPostBack="true"
    > > > DataTextField="CategoryDesc"

    DataValueField="CategoryID"
    > />
    > > >
    > > > </div>
    > > >
    > > > </td>
    > > >
    > > >

    > >
    > >

    >
    >



    ---
    Outgoing mail is certified Virus Free. (well i'd like to think it is.. )
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.723 / Virus Database: 479 - Release Date: 19/07/2004
     
    Mike Smith, Jul 20, 2004
    #4
  5. Moe Sizlak

    Moe Sizlak Guest

    Hi Mike,

    if I use:
    response.write (lstCategorie.SelectedItem.Value)
    I get the error:
    Object reference not set to an instance of an object.

    if I use:
    response.write (lstCategorie.SelectedItem.Text)
    I get the erorr:
    Object reference not set to an instance of an object.

    if I use:
    response.write (lstCategorie.SelectedValue.ToString)
    I get nothing

    Any other ideas?


    "Mike Smith" <> wrote in message
    news:...
    > geez sorry i must have dozed off !
    > its SelectedItem.Text and SelectedItem.Value
    > there is no SelectText.. i merged it myself.. hahahahaha.. sorry..
    >
    > Response.Write(ListBox1.SelectedItem.Value)
    >
    > Response.Write(ListBox1.SelectedItem.Text)
    >
    > Response.Write(ListBox1.SelectedValue.ToString)
    >
    >
    > --
    > Regards,
    > Mike
    > http://bikesh.europe.webmatrixhosting.net
    > http://www.planetsourcecode.com (search keyword: phoetus)
    >
    > "Moe Sizlak" <> wrote in message
    > news:...
    > > Hi Mike,
    > >
    > > I have tried this and it returns 2 different results:
    > > for
    > > lstCategorie.SelectedText.Value
    > > BC30456: 'SelectedText' is not a member of
    > > 'System.Web.UI.WebControls.ListBox'.
    > >
    > > for
    > > lstCategorie.SelectedValue
    > > I get nothing at all from the control
    > >
    > > Any other ideas?
    > >
    > > Moe
    > >
    > >
    > >
    > > "Mike Smith" <> wrote in message
    > > news:#...
    > > > list controls have a SelectedText and a SelectedValue property... u

    can
    > > use
    > > > both actually
    > > >
    > > > ListName.SelectedText.Value
    > > > or
    > > > ListName.SelectedValue
    > > >
    > > > SelectedText.Text gives u the selected text on the listbox
    > > >
    > > > --
    > > > Regards,
    > > > Mike
    > > > http://bikesh.europe.webmatrixhosting.net
    > > > http://www.planetsourcecode.com (search keyword: phoetus)
    > > >
    > > > "Moe Sizlak" <> wrote in message
    > > > news:%...
    > > > > Hi There,
    > > > >
    > > > > I am trying to return the value of a listbox control that is

    included
    > as
    > > a
    > > > > user control, I can return the name of the control but I can't

    access
    > > the
    > > > > integer value of the selected item, what do I need to do in order to
    > > > return
    > > > > the "option value" of the control?
    > > > >
    > > > > Moe
    > > > >
    > > > > !--- returned value of the control
    > > > >
    > > > > _ctl1_lstCategorie
    > > > >
    > > > >
    > > > > !-- compiled page code
    > > > >
    > > > > <select name="_ctl1:lstCategorie" size="1"
    > > > > onchange="__doPostBack('_ctl1$lstCategorie','')"

    language="javascript"
    > > > > id="_ctl1_lstCategorie">
    > > > > <option value="0">-- Choose a Category --</option>
    > > > > <option value="6">...Control Valves
    > > > > ...</option>
    > > > > <option value="10">...Dripline
    > > > > ...</option>
    > > > > </select>
    > > > >
    > > > >
    > > > >
    > > > > !--- page code
    > > > > <%@ Control Language="vb" EnableViewState="true" %>
    > > > >
    > > > > <% @Import Namespace="System.Data" %>
    > > > > <% @Import Namespace="System.Data.SqlClient" %>
    > > > >
    > > > > Public Sub Page_Load(sender as Object, e as EventArgs)
    > > > >
    > > > > Public ReadOnly Property SelectedCategoriesID As String
    > > > > Get
    > > > > return lstCategorie.ClientID
    > > > > End Get
    > > > > End Property
    > > > >
    > > > > If Not Page.IsPostBack Then
    > > > > BindStates()
    > > > > LoadCategories()
    > > > >
    > > > >
    > > > > response.write (lstCategorie.ClientID)
    > > > >
    > > > > end if
    > > > > End Sub
    > > > >
    > > > > Public Sub LoadCategories()
    > > > > Try
    > > > > Dim myConnection as New
    > > > > SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
    > > > > Const strSQL as String = "SELECT CategoryID, '...' + CategoryDesc

    +
    > > > '...'
    > > > > AS CategoryDesc " & _
    > > > > "FROM tblProductCategories ORDER BY CategoryDesc"
    > > > > Dim myCommand as New SqlCommand(strSQL, myConnection)
    > > > > myConnection.Open()
    > > > > Dim objDR as SqlDataReader
    > > > > objDR = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    > > > > lstCategorie.DataSource = objDR
    > > > > lstCategorie.DataBind()
    > > > > lstCategorie.Items.Insert(0, new ListItem("-- Choose a
    > > > Category --","0"))
    > > > >
    > > > > Catch ex as InvalidCastException
    > > > > Status.Text = ex.ToString()
    > > > >
    > > > > Catch ex As SqlException
    > > > > Status.Text = "Database error: " & ex.message
    > > > >
    > > > > Catch ex As Exception
    > > > > Status.Text = "General error: " & ex.message
    > > > > End Try
    > > > >
    > > > > End Sub
    > > > >
    > > > > !---page code
    > > > > <table width="100%" border="0" cellspacing="0" cellpadding="5"
    > > > height="20">
    > > > >
    > > > > <td height="20" width="11%">
    > > > > <div align="right"><font face="Verdana, Arial, Helvetica,
    > > > sans-serif"
    > > > > size="1" color="#FFFFFF">Browse
    > > > > :</font></div>
    > > > > </td>
    > > > > <td height="20" width="14%">
    > > > >
    > > > > <div align="left">
    > > > > <asp:listbox id="lstCategorie" runat="server" Rows="1"
    > > > > AutoPostBack="true"
    > > > > DataTextField="CategoryDesc"

    > DataValueField="CategoryID"
    > > />
    > > > >
    > > > > </div>
    > > > >
    > > > > </td>
    > > > >
    > > > >
    > > > list controls have a SelectedText and a SelectedValue property... u

    can
    > > use
    > > > both actually
    > > >
    > > > ListName.SelectedText.Value
    > > > or
    > > > ListName.SelectedValue
    > > >
    > > > SelectedText.Text gives u the selected text on the listbox
    > > >
    > > > --
    > > > Regards,
    > > > Mike
    > > > http://bikesh.europe.webmatrixhosting.net
    > > > http://www.planetsourcecode.com (search keyword: phoetus)
    > > >
    > > > "Moe Sizlak" <> wrote in message
    > > > news:%...
    > > > > Hi There,
    > > > >
    > > > > I am trying to return the value of a listbox control that is

    included
    > as
    > > a
    > > > > user control, I can return the name of the control but I can't

    access
    > > the
    > > > > integer value of the selected item, what do I need to do in order to
    > > > return
    > > > > the "option value" of the control?
    > > > >
    > > > > Moe
    > > > >
    > > > > !--- returned value of the control
    > > > >
    > > > > _ctl1_lstCategorie
    > > > >
    > > > >
    > > > > !-- compiled page code
    > > > >
    > > > > <select name="_ctl1:lstCategorie" size="1"
    > > > > onchange="__doPostBack('_ctl1$lstCategorie','')"

    language="javascript"
    > > > > id="_ctl1_lstCategorie">
    > > > > <option value="0">-- Choose a Category --</option>
    > > > > <option value="6">...Control Valves
    > > > > ...</option>
    > > > > <option value="10">...Dripline
    > > > > ...</option>
    > > > > </select>
    > > > >
    > > > >
    > > > >
    > > > > !--- page code
    > > > > <%@ Control Language="vb" EnableViewState="true" %>
    > > > >
    > > > > <% @Import Namespace="System.Data" %>
    > > > > <% @Import Namespace="System.Data.SqlClient" %>
    > > > >
    > > > > Public Sub Page_Load(sender as Object, e as EventArgs)
    > > > >
    > > > > Public ReadOnly Property SelectedCategoriesID As String
    > > > > Get
    > > > > return lstCategorie.ClientID
    > > > > End Get
    > > > > End Property
    > > > >
    > > > > If Not Page.IsPostBack Then
    > > > > BindStates()
    > > > > LoadCategories()
    > > > >
    > > > >
    > > > > response.write (lstCategorie.ClientID)
    > > > >
    > > > > end if
    > > > > End Sub
    > > > >
    > > > > Public Sub LoadCategories()
    > > > > Try
    > > > > Dim myConnection as New
    > > > > SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
    > > > > Const strSQL as String = "SELECT CategoryID, '...' + CategoryDesc

    +
    > > > '...'
    > > > > AS CategoryDesc " & _
    > > > > "FROM tblProductCategories ORDER BY CategoryDesc"
    > > > > Dim myCommand as New SqlCommand(strSQL, myConnection)
    > > > > myConnection.Open()
    > > > > Dim objDR as SqlDataReader
    > > > > objDR = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    > > > > lstCategorie.DataSource = objDR
    > > > > lstCategorie.DataBind()
    > > > > lstCategorie.Items.Insert(0, new ListItem("-- Choose a
    > > > Category --","0"))
    > > > >
    > > > > Catch ex as InvalidCastException
    > > > > Status.Text = ex.ToString()
    > > > >
    > > > > Catch ex As SqlException
    > > > > Status.Text = "Database error: " & ex.message
    > > > >
    > > > > Catch ex As Exception
    > > > > Status.Text = "General error: " & ex.message
    > > > > End Try
    > > > >
    > > > > End Sub
    > > > >
    > > > > !---page code
    > > > > <table width="100%" border="0" cellspacing="0" cellpadding="5"
    > > > height="20">
    > > > >
    > > > > <td height="20" width="11%">
    > > > > <div align="right"><font face="Verdana, Arial, Helvetica,
    > > > sans-serif"
    > > > > size="1" color="#FFFFFF">Browse
    > > > > :</font></div>
    > > > > </td>
    > > > > <td height="20" width="14%">
    > > > >
    > > > > <div align="left">
    > > > > <asp:listbox id="lstCategorie" runat="server" Rows="1"
    > > > > AutoPostBack="true"
    > > > > DataTextField="CategoryDesc"

    > DataValueField="CategoryID"
    > > />
    > > > >
    > > > > </div>
    > > > >
    > > > > </td>
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    > ---
    > Outgoing mail is certified Virus Free. (well i'd like to think it is.. )
    > Checked by AVG anti-virus system (http://www.grisoft.com).
    > Version: 6.0.723 / Virus Database: 479 - Release Date: 19/07/2004
    >
    >
     
    Moe Sizlak, Jul 20, 2004
    #5
    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. Matthew Sajdera

    Listbox Control Not Returning Selected Values

    Matthew Sajdera, Apr 12, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    445
    Matthew Sajdera
    Apr 12, 2004
  2. Joey
    Replies:
    2
    Views:
    1,173
  3. K B
    Replies:
    2
    Views:
    702
  4. Divya
    Replies:
    2
    Views:
    326
    Andrew Backer
    Aug 1, 2005
  5. Kiran More
    Replies:
    2
    Views:
    344
    Kiran More
    Nov 14, 2006
Loading...

Share This Page