Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default DataBound ListBox

    I have the following code (this is a snippet):

    InitDBConn();
    OpenDBConnection();

    OleDbCommand cmdRidge = new OleDbCommand("SELECT * FROM
    TRDRidgeTypes", oleDbConn);

    OleDbDataReader oleRidges= cmdRidge.ExecuteReader();

    if (!this.IsPostBack)
    {
    ddlRidge.DataSource = oleRidges;
    ddlRidge.DataMember = "TRDRidgeTypes";
    ddlRidge.DataTextField = "RidgeType";
    ddlRidge.DataValueField = "RidgeTypeID";
    ddlRidge.DataBind();
    }


    ----
    This code successfully fills a DropDownList box. However, when the
    user selects one of the items and the page is posted to the server
    ,the SelectedItem property of the List is null and the SelectedIndex i
    (-1). I am tearing my hair out because I have found people on the
    newsgroup with a similar
    problem, but it was due to their not setting data source in the
    "this.PostBack =False" condition. I have done that, and it still does
    not help me.

    I do have ViewState for the control set to True. I tried hardcoding
    the ListItems in the HTML and the control values were read correctly.

    I am sure I am missing the obvious here..but please help!!

    Thanks,
    Stacey
    anastasia Guest

  2. Similar Questions and Discussions

    1. listbox to populate a listbox
      I am trying to use a listbox that I have setup using flash remoting call to a db. What I want to happen is when you click on a item in first the...
    2. click listbox and refresh another listbox
      Can someone guide me to a resource on building set of drill-down listboxes? Basically I want to have 4 listboxes. The first starts out with...
    3. Item label displays as "," when moving from listbox to listbox
      I have 2 listboxes: "lb_unselected" and "lb_selected". The first thing in the actions is loop through an array populating these two listboxes. ...
    4. now desparate! - 1st listbox contents disappears when 2nd listbox appears?
      On 23 Jun 2003 12:57:45 -0700, KathyBurke40@attbi.com (KathyB) wrote: Its been a while since you posted but I will answer anyway. The problem...
    5. xml & databound (sorting & top 5)
      Hi, I have two small questions concerning databounding an xml file to a datalist. 1. Can i do (ascending/descending) sorting on a certain...
  3. #2

    Default DataBound ListBox

    You don't want to bind the data on post-back.
    You are wiping out the results when you bind the data!

    only bind when postback is TRUE, or when you know the
    content will/should be changing. Otherwise, the viewstate
    will handle everything else for you automatically.

    email if you need more...
    >-----Original Message-----
    >I have the following code (this is a snippet):
    >
    >InitDBConn();
    >OpenDBConnection();
    >
    >OleDbCommand cmdRidge = new OleDbCommand("SELECT * FROM
    >TRDRidgeTypes", oleDbConn);
    >
    >OleDbDataReader oleRidges= cmdRidge.ExecuteReader();
    >
    >if (!this.IsPostBack)
    >{
    >ddlRidge.DataSource = oleRidges;
    >ddlRidge.DataMember = "TRDRidgeTypes";
    >ddlRidge.DataTextField = "RidgeType";
    >ddlRidge.DataValueField = "RidgeTypeID";
    >ddlRidge.DataBind();
    >}
    >
    >
    >----
    >This code successfully fills a DropDownList box. However,
    when the
    >user selects one of the items and the page is posted to
    the server
    >,the SelectedItem property of the List is null and the
    SelectedIndex i
    >(-1). I am tearing my hair out because I have found
    people on the
    >newsgroup with a similar
    >problem, but it was due to their not setting data source
    in the
    >"this.PostBack =False" condition. I have done that, and
    it still does
    >not help me.
    >
    >I do have ViewState for the control set to True. I tried
    hardcoding
    >the ListItems in the HTML and the control values were
    read correctly.
    >
    >I am sure I am missing the obvious here..but please help!!
    >
    >Thanks,
    >Stacey
    >.
    >
    David Waz... Guest

  4. #3

    Default RE: DataBound ListBox

    Stacey is not binding on the postback--at least not in the code below.
    Notice the ! (not) in the IF statement. The code shown says to do the
    binding if it's not a postback.

    We need to see more of the code. What else happens if it IS a postback?
    Do you manually re-populate the dropdownlist if it is a postback. If
    that's the case, then that's the problem.

    -bliz

    --
    Jim Blizzard
    Sr .NET Developer Evangelist
    Microsoft

    Your Potential. Our Passion.

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Please reply to newsgroups only, so that others may benefit. Thanks.


    --------------------
    >Content-Class: urn:content-classes:message
    >From: "David Waz..." <dlw@pickpro.com>
    >Sender: "David Waz..." <dlw@pickpro.com>
    >References: <fdf3218d.0307021116.34878f84@posting.google.com >
    >Subject: DataBound ListBox
    >Date: Wed, 2 Jul 2003 20:20:59 -0700
    >Lines: 57
    >Message-ID: <057501c34112$1f8d8150$a501280a@phx.gbl>
    >MIME-Version: 1.0
    >Content-Type: text/plain;
    > charset="iso-8859-1"
    >Content-Transfer-Encoding: 7bit
    >X-Newsreader: Microsoft CDO for Windows 2000
    >Thread-Index: AcNBEh+LwSBUfYKPT5OjrwgxMvdL4g==
    >X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
    >Newsgroups: microsoft.public.dotnet.framework.aspnet
    >Path: cpmsftngxa09.phx.gbl
    >Xref: cpmsftngxa09.phx.gbl microsoft.public.dotnet.framework.aspnet:31926
    >NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165
    >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    >
    >You don't want to bind the data on post-back.
    >You are wiping out the results when you bind the data!
    >
    >only bind when postback is TRUE, or when you know the
    >content will/should be changing. Otherwise, the viewstate
    >will handle everything else for you automatically.
    >
    >email if you need more...
    >
    >>-----Original Message-----
    >>I have the following code (this is a snippet):
    >>
    >>InitDBConn();
    >>OpenDBConnection();
    >>
    >>OleDbCommand cmdRidge = new OleDbCommand("SELECT * FROM
    >>TRDRidgeTypes", oleDbConn);
    >>
    >>OleDbDataReader oleRidges= cmdRidge.ExecuteReader();
    >>
    >>if (!this.IsPostBack)
    >>{
    >>ddlRidge.DataSource = oleRidges;
    >>ddlRidge.DataMember = "TRDRidgeTypes";
    >>ddlRidge.DataTextField = "RidgeType";
    >>ddlRidge.DataValueField = "RidgeTypeID";
    >>ddlRidge.DataBind();
    >>}
    >>
    >>
    >>----
    >>This code successfully fills a DropDownList box. However,
    >when the
    >>user selects one of the items and the page is posted to
    >the server
    >>,the SelectedItem property of the List is null and the
    >SelectedIndex i
    >>(-1). I am tearing my hair out because I have found
    >people on the
    >>newsgroup with a similar
    >>problem, but it was due to their not setting data source
    >in the
    >>"this.PostBack =False" condition. I have done that, and
    >it still does
    >>not help me.
    >>
    >>I do have ViewState for the control set to True. I tried
    >hardcoding
    >>the ListItems in the HTML and the control values were
    >read correctly.
    >>
    >>I am sure I am missing the obvious here..but please help!!
    >>
    >>Thanks,
    >>Stacey
    >>.
    >>
    >
    Jim Blizzard [MSFT] Guest

  5. #4

    Default Re: DataBound ListBox

    Hi Stacey,

    I was having this exact same problem, and it was almost hair-tearing
    time for me. My issue turned out to be that I had the drop down list in
    a panel, and I had turned ViewState off for that panel. Hence the drop
    down list wasn't putting it's stuff into the ViewState, so when the page
    loaded back up the list was empty.

    So make sure you check -
    * That ViewState is enabled for the drop down list
    * ViewState is enabled for all parent containers (ie. panels,
    placeholders, etc)
    * ViewState is enabled for the page

    Hope that helps -
    gerrod




    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Gerrod Thomas Guest

  6. #5

    Default Re: DataBound ListBox

    Stacey -

    No, I'm not binding on post-back either - I am binding on page load
    only. But the problem was that since the data was not being put into the
    ViewState (since it was disabled by the holding panel), on post back the
    drop down list was empty.

    gerrod


    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    gerrod Guest

  7. #6

    Default Re: DataBound ListBox

    hi gerrod:

    i was replying to David Waz....

    in any case, I tried your suggestion and you were right! Thank you. I
    did not have view state set for the document. Don't know how that
    happened....

    Thanks again!

    stacey
    anastasia Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139