这是我们强制 ObjectDataSource 这样做的唯一方法吗?
问候,
1)我假设ObjectDataSource仅在第一次请求时自动绑定到数据源,但不会在回发时自动绑定到数据源(否则ObjectDataSource.Selecting事件也会在回发时触发,但事实并非如此):
A) 那么强制 ObjectDataSource 也绑定回发的唯一方法是手动调用 DataBind() 吗?
2) 假设 DropDownList1 将 DataSourceID 设置为 ObjectDataSource1 ,那么第一次加载页面时,ObjectDataSource1 将自动调用 DropDownList1.DataBind() (在 Page.PreRender 事件之后)并插入检索到的数据。
A) 但是如果我们在页面首次加载时也手动调用 DropDownList1.DataBind() 会怎么样:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) DropDownList1.DataBind();
}
ObjectDataSource1 是否会注意到 DropDownList1.DataBind() 已被调用,因此不会自动调用 DropDownList1.DataBind() ?
B) 通常 ObjectDataSource1.Selecting 事件在 Page.Prerender 事件之后触发。但是如果在内部调用 DropDownList1.DataBind() 会怎样Page_Load()?
在这种情况下,ObjectDataSource1.Selecting 事件会在 Page.PreRender 之前触发吗?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,ObjectDataSource1.Selecting 事件会在 Page.PreRender 之前触发吗?
是的,它是在 Page.PreRender 之前调用的。
原因:每个设置了DataSourceID属性的数据绑定控件都会在预渲染事件中调用其DataBind方法,
检查页面生命周期
http://msdn.microsoft.com/en-us/library/ms178472。 aspx
http://dotnetshoutout.com /Data-Binding-Events-for-Data-Bound-Controls-in-ASPNet
由于加载事件是在预渲染之前调用的,并且当调用 databind 方法时,那么在您的情况下,objectdatasource 选择的事件在预渲染之前触发
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