ListBox question

Discussion in 'ASP .Net' started by Rob, Feb 12, 2005.

  1. Rob

    Rob Guest

    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!
    Rob, Feb 12, 2005
    #1
    1. Advertising

  2. > 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
    Ryan Trudelle-Schwarz, Feb 12, 2005
    #2
    1. Advertising

  3. Rob

    Rob Guest

    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!
    Rob, Feb 13, 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. blenderdude
    Replies:
    0
    Views:
    970
    blenderdude
    Aug 3, 2003
  2. DC Gringo
    Replies:
    0
    Views:
    398
    DC Gringo
    Apr 6, 2004
  3. kent
    Replies:
    1
    Views:
    875
    Andrew de la Harpe
    May 3, 2004
  4. Simon Prince
    Replies:
    2
    Views:
    3,321
    =?Utf-8?B?TWljaGFlbCBUYXlsb3I=?=
    Oct 19, 2004
  5. Chris Kettenbach

    Listbox selection to populate another listbox?

    Chris Kettenbach, Jun 15, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    1,645
    Chris Kettenbach
    Jun 16, 2005
Loading...

Share This Page