ListBox and DataSource help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Phijo
    New Member
    • Jul 2008
    • 3

    ListBox and DataSource help

    Hello,

    I am writing a page (C#) that uses one ListBox that retrieves data from a DB using SqlDataSource and the SelectCommand.
    Code:
    <asp:SqlDataSource ID="DataSource1" SelectCommandType=Text runat="server" ConnectionString="<%$ ConnectionStrings:SQLDB %>" />
    <asp:ListBox ID="ListBox1" runat="server" SelectionMode=Single Rows=20 Width="250px" DataSourceID="DataSource1" >
    The Listbox populates fine and also retrieves values fine. I put a button under it that, when pressed, uses the Text of the selected item in this ListBox to query the DB using another SelectCommand of a separate SqlDataSource to retrieve other data and place it in a second ListBox so it is dynamically updated while maintaining the original items in the original ListBox.

    My problem is when I select an item in the second ListBox and attempt to retrieve the data, it always brings the selection to the first one in the list, and making the data I am trying to retrieve set to the first item. Upon doing some searching and debugging, I found that the problem seems to be in updating the SqlDataSource.S electCommand. It seems that when I update it, it repopulates the ListBox correctly but doing anything that causes any type of page loading or interaction causes the selection to either go to index 0 (after ListBox2 repopulates) or jump around (before it gets repopulated).

    Any help, advice, or workarounds would be greatly appreciated.
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    You can have more than one datasource per control. Sounds like you need to control the order of events and/or postbacks in the code behind. HTH.

    Comment

    • Phijo
      New Member
      • Jul 2008
      • 3

      #3
      Originally posted by kenobewan
      You can have more than one datasource per control. Sounds like you need to control the order of events and/or postbacks in the code behind. HTH.
      How would I control the order of events if I want the list to be dynamically updated? Is there a command that causes it to reload with the new DataSource? If there is something wrong with the order things are being executed, then why is the list being populated with the correct items?

      I tried editing my code so that now I have:
      Code:
      AutoPostBack=true OnSelectedIndexChanged="SelectItem"
      However, it does the exact same thing as before where it only reports the first item in the selection. After changing the "SqlDataSource. SelectCommand" I can output it to an "asp:GridVi ew" and it dynamically updates perfectly fine every single time I select something from the first (and second) "asp:ListBo x".

      I'm not exactly sure what needs to be done to fix this problem. Perhaps I am not understanding how some of these items work?

      Comment

      Working...