Calling DataBind() multiple times-- strange side effects?

Discussion in 'ASP .Net' started by Jim Bancroft, Dec 28, 2004.

  1. Jim Bancroft

    Jim Bancroft Guest

    Hi everyone,

    I have a DropDownList I populate as outlined below. This is from my
    code-behind file:

    private void Page_Load(object sender, System.EventArgs e)
    {
    BindMyData();
    DataBind();
    }

    private void BindMyData()
    {
    DropDownList2.DataSource = ADONetDataSet.Tables[0];
    DropDownList2.DataValueField="ValueField";
    DropDownList2.DataTextField="TextField";
    DropDownList2.SelectedIndex = 12;
    DropDownList2.DataBind();
    }


    Funny thing: my DropDownList's "SelectedIndex" value won't take hold if I
    call the page level DataBind() method above. If I comment out the method,
    the SelectedIndex value is set properly.

    I've seen other strange behavior when mixing page-level and control-level
    DataBind() calls; for example, if I loop through a DataGrid containing
    DropDownLists and bind each DropDownList individually, they aren't populated
    if I end with a page-level call to DataBind(). As in the previous example,
    if I cut that line out they work as advertised.

    I'm no expert on when to call DataBind() and under what circumstances I
    should call the control vs. page level method. Can anyone provide tips or
    best practices regarding this, along with theories as to what's happening in
    my code snippet? Thanks very much.
    Jim Bancroft, Dec 28, 2004
    #1
    1. Advertising

  2. Hi Jim,
    Bind your data first! before you set the selected index and it should be ok...
    As shown in ur code you are trying to set the selected index before
    databinding, you should do it the other way around...
    Hth...
    R. Thomas
    =?Utf-8?B?Ui4gVGhvbWFzLCBha2EgWHRyZW1lLk5ldA==?=, Dec 28, 2004
    #2
    1. Advertising

  3. "Jim Bancroft" wrote:

    > Hi everyone,
    >
    > I have a DropDownList I populate as outlined below. This is from my
    > code-behind file:
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > BindMyData();
    > DataBind();
    > }
    >
    > private void BindMyData()
    > {
    > DropDownList2.DataSource = ADONetDataSet.Tables[0];
    > DropDownList2.DataValueField="ValueField";
    > DropDownList2.DataTextField="TextField";
    > DropDownList2.SelectedIndex = 12;
    > DropDownList2.DataBind();
    > }
    >
    >
    > Funny thing: my DropDownList's "SelectedIndex" value won't take hold if I
    > call the page level DataBind() method above. If I comment out the method,
    > the SelectedIndex value is set properly.
    >
    > I've seen other strange behavior when mixing page-level and control-level
    > DataBind() calls; for example, if I loop through a DataGrid containing
    > DropDownLists and bind each DropDownList individually, they aren't populated
    > if I end with a page-level call to DataBind(). As in the previous example,
    > if I cut that line out they work as advertised.
    >
    > I'm no expert on when to call DataBind() and under what circumstances I
    > should call the control vs. page level method. Can anyone provide tips or
    > best practices regarding this, along with theories as to what's happening in
    > my code snippet? Thanks very much.
    >
    >
    >
    >


    I have also confronted with similar unexpected behavior of data binding.

    As per my understanding:

    You only need to call Page level DataBind if you need to access some
    property or variable in your WebForm through <%# %> syntax.

    As Page is the parent control which consists of all other Controls of your
    page like datagrid etc. so when you call DataBind on page control it
    recursively calls DataBind methods of child controls.

    And so if you call the DataBind after setting child controls your child
    controls data will be lost.

    Please verify, as this is all my understanding.

    Cheers,
    Rahul Anand
    =?Utf-8?B?UmFodWwgQW5hbmQ=?=, Dec 28, 2004
    #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. Arjen
    Replies:
    1
    Views:
    463
    Curt_C [MVP]
    Aug 17, 2005
  2. Jack Steven
    Replies:
    2
    Views:
    415
    Chris Rebert
    Mar 9, 2009
  3. Michael
    Replies:
    0
    Views:
    210
    Michael
    Dec 29, 2003
  4. Avdi B. Grimm
    Replies:
    0
    Views:
    94
    Avdi B. Grimm
    Oct 5, 2005
  5. penguinista

    hashref strange side effects

    penguinista, Dec 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    120
    penguinista
    Dec 24, 2004
Loading...

Share This Page