473,386 Members | 1,819 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,386 software developers and data experts.

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 1846
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.DropDownList1.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****@discussions.microsoft.comwrote in message
news:75**********************************@microsof t.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.DropDownList1.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.

BaseDataBoundControl:
protected internal override void OnPreRender(EventArgs e)
{
this._preRendered = true;
this.EnsureDataBound();
base.OnPreRender(e);
}

protected virtual void EnsureDataBound()
{
try
{
this._throwOnDataPropertyChange = true;
if (this.RequiresDataBinding && ((this.DataSourceID.Length 0) ||
this._requiresBindToNull))
{
this.DataBind();
this._requiresBindToNull = false;
}
}
finally
{
this._throwOnDataPropertyChange = false;
}
}

protected virtual void OnDataPropertyChanged()
{
if (this._throwOnDataPropertyChange)
{
throw new
HttpException(SR.GetString("DataBoundControl_Inval idDataPropertyChange", new
object[] { this.ID }));
}
if (this._inited)
{
this.RequiresDataBinding = 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.SelectedIndex;
if (selectedIndex >= 0)
{
return this.Items[selectedIndex].Value;
}
return string.Empty;
}
set
{
if (this.Items.Count != 0)
{
if ((value == null) || (base.DesignMode && (value.Length == 0)))
{
this.ClearSelection();
return;
}
ListItem item = this.Items.FindByValue(value);
if ((((this.Page != null) && this.Page.IsPostBack) &&
this._stateLoaded) && (item == null))
{
throw new ArgumentOutOfRangeException("value",
SR.GetString("ListControl_SelectionOutOfRange", new object[] { this.ID,
"SelectedValue" }));
}
if (item != null)
{
this.ClearSelection();
item.Selected = true;
}
}
this.cachedSelectedValue = 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****@discussions.microsoft.comwrote in message
news:75**********************************@microsof t.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.DropDownList1.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*****@DONTLIKESPAMwp.plwrote in message
news:D0**********************************@microsof t.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
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...
1
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"...
1
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...
2
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...
6
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...
3
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...
3
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;...
1
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...
0
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 ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.