Re: Populating Mulitiselect Listbox

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Chris Leuty (chris.leuty-at-nospam.brinker.com)
Date: 04/27/04


Date: Tue, 27 Apr 2004 12:23:06 -0500

I did bind the list data in the asp:listbox. What I'm trying to do now is mark certain items as selected, which is found in a different table. To be specific, the listbox contains trades (Plumbing, Electrical, Masonry, etc), and a second table contains the trades that apply to the displayed form (i.e. this vendor does this sort of work). This listbox is part of an Edit Template on a DataList (so that the selections can be changed).

I chose DataBinding() only as an example of where I have tried to get a reference to the listbox (it didn't work). I had to define the listbox in the code (VS.NET did not create a reference to it; I figured it was because it was part of the Edit Template, which is late bound), so I felt that I needed to find a place in the process where the listbox was created, but not yet sent back to the browser. The point where the data is bound seemed to make the most sense to me, but I can't grab a reference to it.

Thanks for your help so far.
  "Scott G." <noemail@this-is-extra-hotmail.com> wrote in message news:eOpgNWHLEHA.2100@TK2MSFTNGP10.phx.gbl...

   A good spot is probably Page_Load; if you are using the default code that VS.NET generates then this method should be there.... again, if you are using VS.NET there should already be a member field call "lstTrades" as a protected member of the code behind for the ASPX page.

   If you want the ListBox during a ListBox DataBinding event; then just CType the sender (the first parent) to the event handler; In C# that might look like (for the ListBox DataBindingEvent):

  private void lstTrades_DataBinding(object sender, EventArgs e)
  {
      ListBox lb = (ListBox)sender;
  }

   I'm a bit confused if you if you are trying to bind the data to the ListBox in an event for the ListBox; if that's what you are asking, then I think you'd be better off binding the data to the ListBox in the containing page.

  Scott
    "Chris Leuty" <chris.leuty-at-nospam.brinker.com> wrote in message news:%23bmnrLHLEHA.892@TK2MSFTNGP09.phx.gbl...
    The listbox is currently in the aspx, bound like this:
              <TD vAlign="top" align="right">
                  <asp:label id="Label13" runat="server" CssClass="form-label">Trades</asp:label>
              </TD>
              <TD>
                  <asp:listbox id="lstTrades" runat="server" CssClass="form-text" Width="154px" SelectionMode="Multiple" DataSource='<%# ContactInfo1 %>' DataMember="Trades" DataTextField="TradeName" DataValueField="TradeID">
                  </asp:listbox>
              </TD>
    I have code similar to your examples in the codebehind, but I'm having a hard time finding a place where the Listbox is available as an object (or perhaps my code is fubar). I'm trying to get the listbox reference for the asp:listbox item above by doing the following (in this case, I was doing it during the event that bound the data. e is a DataListCommandEventArgs):
      lstTrades = CType(e.Item.FindControl("lstTrades"), ListBox)
    I think once I get to that point, the actual selection based on the data will be easy. So far, I haven't found a good place to hook this line (I keep getting back Nothing).

      "Scott G." <noemail@this-is-extra-hotmail.com> wrote in message news:OEfke5FLEHA.3596@tk2msftngp13.phx.gbl...

       I think you should be able to do this with a single table; have you tried something like:

      ListBox lb = new ListBox();
      lb.SelectionMode = ListSelectionMode.Multiple;
      lb.DataSource = whatever;
      lb.DataBind();
      // now set from the list box
      foreach (ListItem item in lb.Items)
      {
       if (IsItemSelected(item.Value)) // IsItemSelected is your method
       {
        item.Selected = true;
       }
      }

      // or do it from the data side
      foreach (DataRow row in whatever)
      {
       // blah, whatever you have to determine what's selected.
       if (row["IsSelected"] == "True")
       {
        lb.Items.FindByValue(row["Value"].ToString()).Selected = true;
       }
      }

      Scott

        "Chris Leuty" <chris.leuty@nospam.briniker.com> wrote in message news:OY02of8KEHA.3216@tk2msftngp13.phx.gbl...
        I am populating a multiselect Listbox from a dataset, with the content of
        the listbox filled by one table, and the selections determined from another
        table. So far, I have been keeping the dataset a denormalized mirror of the
        database, but I'm not having much luck getting the selection logic down (I
        haven't found a 'hook' where I can access the listbox object as an object to
        set the listitem's selected property before it gets rendered)..

        Before denormalizing the data in hopes of simplifying the binding, I thought
        I would ask if I was missing an obviously simple approach to doing what I am
        after (and leaving the dataset denormalized).

        Thanks



Relevant Pages

  • Re: ListBox and PostBackUrl event
    ... Specify the ListBox as ... protected void Page_Load(object sender, EventArgs e) ...
    (microsoft.public.dotnet.framework.aspnet)
  • Question about ASP.NET ServerSide DropDown listbox
    ... On an aspx page I have a drop down list box. ... The vb code behind retrieves data from a SQL Server 2005 table to display in ... I would like to display the ... third value, "Hello", in the listbox and still include all of the other items ...
    (microsoft.public.dotnet.languages.vb.controls)
  • Re: Very Simple question
    ... Scott: Are you pasting the code into the "HTML view" of an ASPX page? ... > Dim objConnection As OleDBConnection ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Everything stops when a Thread is running on the server
    ... protected void _btnLoadTools_Click(object sender, EventArgs e) { ... ASPX worker process. ... the ASPX worker process and wouldn't give up any time to the client. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: getting a file list
    ... Since you want to be able to select several files I would drop the SelectedIndexChanged event, enable multiselect on the ListBox and put a Button that triggers the job. ... private void lBDataFiles_SelectedIndexChanged(object sender, EventArgs e) ...
    (microsoft.public.dotnet.languages.csharp)