DataBound ListBox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anastasia

    DataBound ListBox

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

    InitDBConn();
    OpenDBConnectio n();

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

    OleDbDataReader oleRidges= cmdRidge.Execut eReader();

    if (!this.IsPostBa ck)
    {
    ddlRidge.DataSo urce = oleRidges;
    ddlRidge.DataMe mber = "TRDRidgeTypes" ;
    ddlRidge.DataTe xtField = "RidgeType" ;
    ddlRidge.DataVa lueField = "RidgeTypeI D";
    ddlRidge.DataBi nd();
    }


    ----
    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.PostB ack =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...

    #2
    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...
    [color=blue]
    >-----Original Message-----
    >I have the following code (this is a snippet):
    >
    >InitDBConn() ;
    >OpenDBConnecti on();
    >
    >OleDbCommand cmdRidge = new OleDbCommand("S ELECT * FROM
    >TRDRidgeTypes" , oleDbConn);
    >
    >OleDbDataReade r oleRidges= cmdRidge.Execut eReader();
    >
    >if (!this.IsPostBa ck)
    >{
    >ddlRidge.DataS ource = oleRidges;
    >ddlRidge.DataM ember = "TRDRidgeTypes" ;
    >ddlRidge.DataT extField = "RidgeType" ;
    >ddlRidge.DataV alueField = "RidgeTypeI D";
    >ddlRidge.DataB ind();
    >}
    >
    >
    >----
    >This code successfully fills a DropDownList box. However,[/color]
    when the[color=blue]
    >user selects one of the items and the page is posted to[/color]
    the server[color=blue]
    >,the SelectedItem property of the List is null and the[/color]
    SelectedIndex i[color=blue]
    >(-1). I am tearing my hair out because I have found[/color]
    people on the[color=blue]
    >newsgroup with a similar
    >problem, but it was due to their not setting data source[/color]
    in the[color=blue]
    >"this.PostBa ck =False" condition. I have done that, and[/color]
    it still does[color=blue]
    >not help me.
    >
    >I do have ViewState for the control set to True. I tried[/color]
    hardcoding[color=blue]
    >the ListItems in the HTML and the control values were[/color]
    read correctly.[color=blue]
    >
    >I am sure I am missing the obvious here..but please help!!
    >
    >Thanks,
    >Stacey
    >.
    >[/color]

    Comment

    • Jim Blizzard [MSFT]

      #3
      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.


      --------------------[color=blue]
      >Content-Class: urn:content-classes:message
      >From: "David Waz..." <dlw@pickpro.co m>
      >Sender: "David Waz..." <dlw@pickpro.co m>
      >References: <fdf3218d.03070 21116.34878f84@ posting.google. com>
      >Subject: DataBound ListBox
      >Date: Wed, 2 Jul 2003 20:20:59 -0700
      >Lines: 57
      >Message-ID: <057501c34112$1 f8d8150$a501280 a@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+LwSBUfYK PT5OjrwgxMvdL4g ==
      >X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
      >Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
      >Path: cpmsftngxa09.ph x.gbl
      >Xref: cpmsftngxa09.ph x.gbl microsoft.publi c.dotnet.framew ork.aspnet:3192 6
      >NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165
      >X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.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...
      >[color=green]
      >>-----Original Message-----
      >>I have the following code (this is a snippet):
      >>
      >>InitDBConn( );
      >>OpenDBConnect ion();
      >>
      >>OleDbComman d cmdRidge = new OleDbCommand("S ELECT * FROM
      >>TRDRidgeTypes ", oleDbConn);
      >>
      >>OleDbDataRead er oleRidges= cmdRidge.Execut eReader();
      >>
      >>if (!this.IsPostBa ck)
      >>{
      >>ddlRidge.Data Source = oleRidges;
      >>ddlRidge.Data Member = "TRDRidgeTypes" ;
      >>ddlRidge.Data TextField = "RidgeType" ;
      >>ddlRidge.Data ValueField = "RidgeTypeI D";
      >>ddlRidge.Data Bind();
      >>}
      >>
      >>
      >>----
      >>This code successfully fills a DropDownList box. However,[/color]
      >when the[color=green]
      >>user selects one of the items and the page is posted to[/color]
      >the server[color=green]
      >>,the SelectedItem property of the List is null and the[/color]
      >SelectedInde x i[color=green]
      >>(-1). I am tearing my hair out because I have found[/color]
      >people on the[color=green]
      >>newsgroup with a similar
      >>problem, but it was due to their not setting data source[/color]
      >in the[color=green]
      >>"this.PostBac k =False" condition. I have done that, and[/color]
      >it still does[color=green]
      >>not help me.
      >>
      >>I do have ViewState for the control set to True. I tried[/color]
      >hardcoding[color=green]
      >>the ListItems in the HTML and the control values were[/color]
      >read correctly.[color=green]
      >>
      >>I am sure I am missing the obvious here..but please help!!
      >>
      >>Thanks,
      >>Stacey
      >>.
      >>[/color]
      >[/color]

      Comment

      Working...