473,387 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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("c onnectionString"))
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.CloseConne ction)
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>
Nov 18 '05 #1
4 2417
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" <mo*@moe.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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("c onnectionString"))
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.CloseConne ction)
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" <mo*@moe.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... 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("c onnectionString"))
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.CloseConne ction)
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>

Nov 18 '05 #2
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" <te**@test.com> wrote in message
news:#R**************@TK2MSFTNGP09.phx.gbl...
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" <mo*@moe.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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("c onnectionString"))
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.CloseConne ction)
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" <mo*@moe.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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("c onnectionString"))
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.CloseConne ction)
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>


Nov 18 '05 #3
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" <mo*@moe.com> wrote in message
news:uQ*************@TK2MSFTNGP12.phx.gbl...
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" <te**@test.com> wrote in message
news:#R**************@TK2MSFTNGP09.phx.gbl...
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" <mo*@moe.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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("c onnectionString"))
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.CloseConne ction)
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" <mo*@moe.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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("c onnectionString"))
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.CloseConne ction)
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
Nov 18 '05 #4
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" <te**@test.com> wrote in message
news:u2**************@tk2msftngp13.phx.gbl...
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" <mo*@moe.com> wrote in message
news:uQ*************@TK2MSFTNGP12.phx.gbl...
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" <te**@test.com> wrote in message
news:#R**************@TK2MSFTNGP09.phx.gbl...
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" <mo*@moe.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
> 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("c onnectionString"))
> 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.CloseConne ction)
> 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" <mo*@moe.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
> 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("c onnectionString"))
> 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.CloseConne ction)
> 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

Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Vipin Kedia | last post by:
Hi I have written a code for showing the list boxes as selected using a Listitem and the selected property of the items. Now I have 2 list boxes in my page. But it shows only the selected values...
8
by: Oddball | last post by:
Ok - I have a ListBox control and I'm ready to write my own DrawItem event handler. What I want to draw as the item is another control. I have created a user control that I would like to list in...
1
by: Matthew Sajdera | last post by:
All - I'm trying to capture the values in a multi-select ListBox and everything I've tried isn't working. Here is the code snippet that is trying to grab SELECTED values: // Build Copy To...
1
by: Karen Grube | last post by:
Hi! I'm using a standard server side ASP.Net listbox control on a web form. The page is basically various shades of green. The listbox itself has a pale green background and forest green text...
1
by: BillAtWork | last post by:
Hi, I have a listbox (in a user control) which has items add/removed via javascript (i.e. an "add item" link which uses a popup selection screen to add items and a "remove item" link which deletes...
2
by: sumanthsclsdc | last post by:
Hello friends, I have a problem, I implemented a class which uses tkinter and displays the window as required, the class will create a window with listbox and inserts some items into it, I...
4
by: rn5a | last post by:
I am binding a DropDownList with records existing in a database table. I want to add an extra item *SELECT COMPANY* at index 0 so that by default, it gets selected. This is how I tried it but the...
1
by: stimul8d | last post by:
okay; ASP. I have i listbox inside a user control which is dynamically created on page_init. I check for postback and only populate the datasource if it's false. regardless, i do this ...
1
by: angelicdevil | last post by:
i have listbox 1 which displays status , based on selection of status listbox 2 displays usernames. and based on username selected the textbox displays the email id. its working fine till...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.