Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Calling DataBind() multiple times-- strange side effects?

Reply
Thread Tools

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

 
 
Jim Bancroft
Guest
Posts: n/a
 
      12-28-2004
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.



 
Reply With Quote
 
 
 
 
=?Utf-8?B?Ui4gVGhvbWFzLCBha2EgWHRyZW1lLk5ldA==?=
Guest
Posts: n/a
 
      12-28-2004
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
 
Reply With Quote
 
 
 
 
=?Utf-8?B?UmFodWwgQW5hbmQ=?=
Guest
Posts: n/a
 
      12-28-2004

"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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PIL how to display multiple images side by side suresh.amritapuri Python 4 01-11-2010 10:07 PM
Calling a client side script from server side. =?Utf-8?B?RHVzdGluIElJLg==?= ASP .Net 3 08-16-2004 10:10 PM
Server Side button calling page_load before calling it's own click event. Ryan Ternier ASP .Net 4 07-29-2004 01:06 PM
Calling client side Javascript from a server side button =?Utf-8?B?Qw==?= ASP .Net 1 01-16-2004 05:56 PM
VS.NET 2003 debugging became very slow after installing VS6 side by side Andy Chau ASP .Net 0 11-20-2003 01:19 AM



Advertisments