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

J

Jim Bancroft

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.
 
G

Guest

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
 
G

Guest

Jim Bancroft said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top