Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

When does a Bound Control DataBind?

1 view
Skip to first unread message

Jonathan Wood

unread,
May 8, 2008, 6:59:00 PM5/8/08
to
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

Manish

unread,
May 9, 2008, 8:38:00 AM5/9/08
to
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

unread,
May 9, 2008, 12:00:29 PM5/9/08
to
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" <Man...@discussions.microsoft.com> wrote in message
news:7500218E-8AE1-4609...@microsoft.com...

Milosz Skalecki [MCAD]

unread,
May 9, 2008, 7:30:00 PM5/9/08
to
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_InvalidDataPropertyChange", 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

unread,
May 10, 2008, 12:35:43 PM5/10/08
to
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.

Milosz Skalecki [MCAD]

unread,
May 10, 2008, 6:47:00 PM5/10/08
to
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

unread,
May 10, 2008, 7:22:24 PM5/10/08
to
Cool. Thanks.

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

"Milosz Skalecki [MCAD]" <mil...@DONTLIKESPAMwp.pl> wrote in message
news:D0F112DB-AB67-43B2...@microsoft.com...

0 new messages