问题
Greetings,
1) I assume ObjectDataSource automatically binds to data source only on first request, but not on postbacks ( else ObjectDataSource.Selecting event would be fired on postbacks also, but it isn’t):
A) So only way to force ObjectDataSource to also bind on postbacks is by manually calling DataBind()?
2) Assuming DropDownList1 has DataSourceID set to ObjectDataSource1 , then first time page is loaded, ObjectDataSource1 will automatically call DropDownList1.DataBind() ( after Page.PreRender event) and insert retrieved data.
A) But what if we also manually call DropDownList1.DataBind() when page is first loaded:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) DropDownList1.DataBind();
}
Will ObjectDataSource1 somehow notice that DropDownList1.DataBind() has already be called and thus won’t automatically call DropDownList1.DataBind() ?
B) Normally ObjectDataSource1.Selecting event is fired after Page.Prerender event.But what if DropDownList1.DataBind() is called inside Page_Load()?
Will in that case ObjectDataSource1.Selecting event be fired prior to Page.PreRender?
thanx
回答1:
Will in that case ObjectDataSource1.Selecting event be fired prior to Page.PreRender?
Yes it is called prior to Page.PreRender.
Reason: Each data bound control whose DataSourceID property is set calls its DataBind method in prerender event,
check page life cycle http://msdn.microsoft.com/en-us/library/ms178472.aspx
http://dotnetshoutout.com/Data-Binding-Events-for-Data-Bound-Controls-in-ASPNet
Since the load event is called before prerender, and when call databind method then in your situation objectdatasource selected event fired before prerender
来源:https://stackoverflow.com/questions/1262220/is-this-the-only-way-we-can-force-objectdatasource-to