473,418 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

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

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.

Nov 19 '05 #1
2 1755
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
Nov 19 '05 #2

"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

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Steve Jorgensen | last post by:
== On Error Resume next, and Err.Number == If you want to call one of your procedures from another procedure, and check for errors afterward, you mayimagine that you should write code something...
23
by: Mantorok Redgormor | last post by:
Can emulation of the logical OR be done in standard C to obfuscate its use? So I don't have to use if(a||b) but instead make that even more obfuscated without its use but testing the same condition
9
by: Rouben Rostamian | last post by:
Consider the following illustrative program: #include <stdio.h> double f(double x) { return x*x; } double g(double x)
5
by: Niklaus | last post by:
This is one of the posts that i got. ------------------------------ A "side effect" of an operation is something that *happens*, not something that *is produced*. Examples: In the expression...
2
by: Andrew Arro | last post by:
is it possible to make smth like a loop of macroses? i.e. i want some macros to be called X times, all that on the PREPROCESSOR lever i was trying smth like #define vv_0 100 #define vv_1...
17
by: dingoatemydonut | last post by:
The C99 standard states: "In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce...
2
by: Raul Medina | last post by:
I am trying to print the same visual basic report multiple times in my VB 6.0 application using the command <reportObject>.printReport False, rptRangeAllPages. The only thing that differs between...
4
by: aziz001 | last post by:
I have a form where a person can select how many of a product they want e.g. 1x2g Syringe, 15x100g jar etc. The code will then display string output in a label, listing the products that have a...
20
by: copx | last post by:
Is it true that C macros are unsafe when combined with side effects even if they are "clean" e.g. could foo_macro(++c); execute "++c" multiple times even if foo_macro is "clean"? I have always...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.