473,545 Members | 2,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When does a Bound Control DataBind?

I have a databound dropdownlist control. Based on some other criteria, I
need to specify the selected item in my pages Load event.

The problem is that, in my load event, the control does not yet have any
data. I've found I can call DataBind() on that control and then it works
okay. However, this has me wondering where the control normally databinds,
and if me doing it manually would actually introduce the overhead of having
the control databind twice.

Can anyone answer these questions?

1. Does a control know it's been databound such that it will not repeat the
process unecessarily?

2. Is there a better way to specify the selected value of a control that has
not yet databound?

Thanks!

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

Jun 27 '08 #1
6 1854
Hi Jonathan,

Please know that if you bind the dropdownlist control at design time with
some datasource then also specify also specify its text and value fields to
one of the column of the table. then you do not need to bind the dropdownlist
at run time.

Also, you can set the Selected Value of the control by calling

Me.DropDownList 1.SelectedValue = 76

but for that dropdownlist control should be bound.

Regards,
Manish
www.componentone.com

"Jonathan Wood" wrote:
I have a databound dropdownlist control. Based on some other criteria, I
need to specify the selected item in my pages Load event.

The problem is that, in my load event, the control does not yet have any
data. I've found I can call DataBind() on that control and then it works
okay. However, this has me wondering where the control normally databinds,
and if me doing it manually would actually introduce the overhead of having
the control databind twice.

Can anyone answer these questions?

1. Does a control know it's been databound such that it will not repeat the
process unecessarily?

2. Is there a better way to specify the selected value of a control that has
not yet databound?

Thanks!

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

Jun 27 '08 #2
Thanks, but as I described, my page's Load event cannot set the selected
value because the control has not yet been databound.

My question relates to calling the control's DataBind() method. If I do
that, then the control has data. But my concern is about performance if the
control automatically performs DataBind() before the page is finished, which
would mean it happens twice.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"Manish" <Ma****@discuss ions.microsoft. comwrote in message
news:75******** *************** ***********@mic rosoft.com...
Hi Jonathan,

Please know that if you bind the dropdownlist control at design time with
some datasource then also specify also specify its text and value fields
to
one of the column of the table. then you do not need to bind the
dropdownlist
at run time.

Also, you can set the Selected Value of the control by calling

Me.DropDownList 1.SelectedValue = 76

but for that dropdownlist control should be bound.

Regards,
Manish
www.componentone.com

"Jonathan Wood" wrote:
>I have a databound dropdownlist control. Based on some other criteria, I
need to specify the selected item in my pages Load event.

The problem is that, in my load event, the control does not yet have any
data. I've found I can call DataBind() on that control and then it works
okay. However, this has me wondering where the control normally
databinds,
and if me doing it manually would actually introduce the overhead of
having
the control databind twice.

Can anyone answer these questions?

1. Does a control know it's been databound such that it will not repeat
the
process unecessarily?

2. Is there a better way to specify the selected value of a control that
has
not yet databound?

Thanks!

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

Jun 27 '08 #3
Hi Jomathan,

Ad 1. Yes, it does know.

BaseDataBoundCo ntrol:
protected internal override void OnPreRender(Eve ntArgs e)
{
this._preRender ed = true;
this.EnsureData Bound();
base.OnPreRende r(e);
}

protected virtual void EnsureDataBound ()
{
try
{
this._throwOnDa taPropertyChang e = true;
if (this.RequiresD ataBinding && ((this.DataSour ceID.Length 0) ||
this._requiresB indToNull))
{
this.DataBind() ;
this._requiresB indToNull = false;
}
}
finally
{
this._throwOnDa taPropertyChang e = false;
}
}

protected virtual void OnDataPropertyC hanged()
{
if (this._throwOnD ataPropertyChan ge)
{
throw new
HttpException(S R.GetString("Da taBoundControl_ InvalidDataProp ertyChange", new
object[] { this.ID }));
}
if (this._inited)
{
this.RequiresDa taBinding = true;
}
}

As you can see data is bound only once (for the same datasource parameters).

Ad 2.

You can always set SelectedValue in the Page_load, even before the data has
been bound, as the SelectedValue is stored in the temporary variable until
the next databinding:

ListControl (base class for lis type control, i.e. dropdownlist, bulletedlist)
public virtual string SelectedValue
{
get
{
int selectedIndex = this.SelectedIn dex;
if (selectedIndex >= 0)
{
return this.Items[selectedIndex].Value;
}
return string.Empty;
}
set
{
if (this.Items.Cou nt != 0)
{
if ((value == null) || (base.DesignMod e && (value.Length == 0)))
{
this.ClearSelec tion();
return;
}
ListItem item = this.Items.Find ByValue(value);
if ((((this.Page != null) && this.Page.IsPos tBack) &&
this._stateLoad ed) && (item == null))
{
throw new ArgumentOutOfRa ngeException("v alue",
SR.GetString("L istControl_Sele ctionOutOfRange ", new object[] { this.ID,
"SelectedVa lue" }));
}
if (item != null)
{
this.ClearSelec tion();
item.Selected = true;
}
}
this.cachedSele ctedValue = value;
}
}

HTH
--
Milosz
"Jonathan Wood" wrote:
Thanks, but as I described, my page's Load event cannot set the selected
value because the control has not yet been databound.

My question relates to calling the control's DataBind() method. If I do
that, then the control has data. But my concern is about performance if the
control automatically performs DataBind() before the page is finished, which
would mean it happens twice.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"Manish" <Ma****@discuss ions.microsoft. comwrote in message
news:75******** *************** ***********@mic rosoft.com...
Hi Jonathan,

Please know that if you bind the dropdownlist control at design time with
some datasource then also specify also specify its text and value fields
to
one of the column of the table. then you do not need to bind the
dropdownlist
at run time.

Also, you can set the Selected Value of the control by calling

Me.DropDownList 1.SelectedValue = 76

but for that dropdownlist control should be bound.

Regards,
Manish
www.componentone.com

"Jonathan Wood" wrote:
I have a databound dropdownlist control. Based on some other criteria, I
need to specify the selected item in my pages Load event.

The problem is that, in my load event, the control does not yet have any
data. I've found I can call DataBind() on that control and then it works
okay. However, this has me wondering where the control normally
databinds,
and if me doing it manually would actually introduce the overhead of
having
the control databind twice.

Can anyone answer these questions?

1. Does a control know it's been databound such that it will not repeat
the
process unecessarily?

2. Is there a better way to specify the selected value of a control that
has
not yet databound?

Thanks!

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com


Jun 27 '08 #4
Milosz,
As you can see data is bound only once (for the same datasource
parameters).
Thanks, that's what I was wondering about. I'm not sure if I understood how,
but I've printed out your reply and will examine the code more closely.

May I ask where you got that listing? I was thinking the framework source
was unavailable. What's the trick?
You can always set SelectedValue in the Page_load, even before the data
has
been bound, as the SelectedValue is stored in the temporary variable until
the next databinding:
Okay, I may need the data to correctly determine which item should be
selected. But that's helpful to know the SelectedValue can be set first.

Thanks again.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

Jun 27 '08 #5
Hi Jonathan,

I used Reflector, very powerful freeware reverse engineering tool.
http://www.aisto.com/roeder/dotnet/
It will help you to understand what happens under the hood.
Have a nice weekend.

--
Milosz
"Jonathan Wood" wrote:
Milosz,
As you can see data is bound only once (for the same datasource
parameters).

Thanks, that's what I was wondering about. I'm not sure if I understood how,
but I've printed out your reply and will examine the code more closely.

May I ask where you got that listing? I was thinking the framework source
was unavailable. What's the trick?
You can always set SelectedValue in the Page_load, even before the data
has
been bound, as the SelectedValue is stored in the temporary variable until
the next databinding:

Okay, I may need the data to correctly determine which item should be
selected. But that's helpful to know the SelectedValue can be set first.

Thanks again.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

Jun 27 '08 #6
Cool. Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"Milosz Skalecki [MCAD]" <mi*****@DONTLI KESPAMwp.plwrot e in message
news:D0******** *************** ***********@mic rosoft.com...
Hi Jonathan,

I used Reflector, very powerful freeware reverse engineering tool.
http://www.aisto.com/roeder/dotnet/
It will help you to understand what happens under the hood.
Have a nice weekend.

--
Milosz
"Jonathan Wood" wrote:
>Milosz,
As you can see data is bound only once (for the same datasource
parameters).

Thanks, that's what I was wondering about. I'm not sure if I understood
how,
but I've printed out your reply and will examine the code more closely.

May I ask where you got that listing? I was thinking the framework source
was unavailable. What's the trick?
You can always set SelectedValue in the Page_load, even before the data
has
been bound, as the SelectedValue is stored in the temporary variable
until
the next databinding:

Okay, I may need the data to correctly determine which item should be
selected. But that's helpful to know the SelectedValue can be set first.

Thanks again.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

Jun 27 '08 #7

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

Similar topics

8
7893
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the header of the datagrid in the "ItemDataBound" method of the grid. However, once, they are added in the grid, i seem to lose the event handler for the...
1
3994
by: Jax | last post by:
I have an arraylist of objects. This arraylist is bound to a repeater. That repeater then creates a set of controls like so <asp:repeater id="garmentRepeater" runat="server" OnItemCommand="Repeater_ButtonClick"><HeaderTemplate><asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 250px; POSITION: absolute; TOP: 80px" runat="server Width="200px"...
1
1541
by: Anand Sagar | last post by:
The DataBind I understand is useful for binding data from datasources to WebControls like dropdownlist, datagrid etc. usually during the Page_Load or Page_Init In what cases will anyone use a Page.DataBind ? Thanks, Anand Sagar
2
2024
by: Andy Fish | last post by:
Hi, First some background: When you databind a repeater control, the controls within the template are given an id like Repeater1:_ctl<n>:Button1 where <n> increments for each repeater item. If you re-bind the control later on in the page lifecycle, the contents are re-generated with the IDs starting from 0 again. I have written my own...
6
2304
by: Nathan Sokalski | last post by:
I am using a DataSet as the DataSource of a DataList in my code. The SQL used to get the data from the database begins with: SELECT members.organization,artists.artist,artists.email,artists.website,members.email FROM members INNER JOIN artists ON members.memberid=artists.memberid WHERE Notice that both tables involved in the SELECT...
3
4090
by: Seagull Ng | last post by:
Hello all, I am doing my web-based reporting tool in ASP.Net 2.0 using Visual Web Developer 2005. I try to export my populated gridview to Excel spreadsheet but what I got is just a blank spreadsheet with only <div></div> tag in the first cell. Can anyone tell me what actually is the problem? Thanks in advance.
3
1658
by: spamguy | last post by:
I have a DropDownList set up and bound to an SQL query: sqlConnection1.Open(); SqlDataReader db_reader = getAllRoles.ExecuteReader(); selectRole.DataSource = db_reader; selectRole.DataBind(); db_reader.Close();
1
5273
by: bogdan | last post by:
I need to execute some code _after_ page controls are bound to data (e.g. DropDownList). I could probably handle DataBound events for each control. But if I wanted to place the code in a page handler, where would I put it? Does Page_Load() event is raised after or before control data binding takes place? Thanks, Bogdan
0
1986
by: aboutjav.com | last post by:
Hi, I need some help. I am getting this error after I complete the asp.net register control and click on the continue button. It crashed when it tries to get it calls this Profile property ((string)(this.GetPropertyValue("Address1")));
0
7502
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7692
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7946
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6026
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5360
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3491
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.