ListBox question

R

Rob

I have a listbox which is populated by a dataset:
'initiate the DataSet and all the rest here

my sql statement is this:
SELECT company_id, name, description FROM companies

ds = DataControl.GetDataSet(sql)
lstCompanies.DataSource = ds
lstCompanies.DataTextField = "name"
lstCompanies.DataValueField = "company_id"
lstCompanies.DataBind()

When I click on a listItem, I want to be able to show the description
(which is in the DataSet) of that company on a label beside the listbox.
Is there anyway of doing this? The listbox doesn't even have a Click
event.

Thanks

Rob
 
R

Ryan Trudelle-Schwarz

I have a listbox which is populated by a dataset:
'initiate the DataSet and all the rest here
my sql statement is this:
SELECT company_id, name, description FROM companies
ds = DataControl.GetDataSet(sql)
lstCompanies.DataSource = ds
lstCompanies.DataTextField = "name"
lstCompanies.DataValueField = "company_id"
lstCompanies.DataBind()
When I click on a listItem, I want to be able to show the description
(which is in the DataSet) of that company on a label beside the
listbox. Is there anyway of doing this? The listbox doesn't even have
a Click event.

What I've done in the past is pass down an array of id and description values
then added a handler for the onchange of the <select> element (should be
what the listbox renders as so you could likely add an attribute to the listbox
for the onchange) call a method to update the label (or in my case a textbox).
The rendered output should look something like so:

<!-- Output this using a Page.RegisterClientScriptBlock call) after you bind
-->
<script type="text/javascript">var listboxId_values = [[id1,desc1],[id2,desc2]];</script>

<!-- This should resemble the output of the ListBox control with an onchange
attribute added -->
<select id="listboxId" onchange="updateRelated('listboxId', 'labelId', listboxId_values);">
...
</select>

<!-- This should resemble the output of a Label -->
<span id="labelId"></span>

<!-- throw this anywhere it will be included on the page -->
<script type="text/javascript">
function updateRelated(sid, rid, values)
{
// select element
var s = document.getElementById(sid);
// target
var r = document.getElementById(rid);

var v = s.value;
var i = 0;

while(i < values.length && values[0] != v)
i++;

if(i < values.length)
{
r.innerText = values[1];
}
}
</script>

hth, Ryan
 
R

Rob

Thanks Ryan,
I can't seem to call Page.RegisterClientScriptBlock. It's not available
as an option. Do I need to Import something?

I'm new to .Net

Rob
 

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

Similar Threads


Members online

No members online now.

Forum statistics

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

Latest Threads

Top