Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ListBox question

Reply
Thread Tools

ListBox question

 
 
Rob
Guest
Posts: n/a
 
      02-12-2005
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




*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
 
Ryan Trudelle-Schwarz
Guest
Posts: n/a
 
      02-12-2005
> 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[i][0] != v)
i++;

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

hth, Ryan


 
Reply With Quote
 
 
 
 
Rob
Guest
Posts: n/a
 
      02-13-2005
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



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Listbox selection to populate another listbox? Chris Kettenbach ASP .Net 3 06-16-2005 09:19 PM
Listbox item added with client scripts not submitting with ASP:Listbox Simon Prince ASP .Net 2 10-19-2004 04:11 PM
How do I move all items in a listbox to another listbox kent ASP .Net 1 05-03-2004 12:17 AM
click listbox and refresh another listbox DC Gringo ASP .Net 0 04-06-2004 02:13 AM
Re: now desparate! - 1st listbox contents disappears when 2nd listbox appears? blenderdude ASP .Net 0 08-03-2003 10:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57