背景:
如页面A,A上含有用户控件B,目标是刚进入A页面就立即调用用户控件B中方法。
刚开始直接在A页面page_load里调用导致出错。
出错原因:与页面生命周期有关,真实情况是首先执行A页面的page_load,完毕后再执行B页面的page_load。而当直接写在A的page_load中,则导致先调用了B的方法,再执行B的page_load,顺序错误出现莫名其妙的错(我的报错:“SelectedIndex”和“SelectedValue”属性互相排斥。)。
解决方法:
控件B中方法的调用写在A页面的page_load之后,即重写OnPreRender方法。
这时需要了解页面执行顺序:http://www.cnblogs.com/yeminglong/archive/2012/10/16/2725664.html
1. OnPreInit
2. OnInit
3. OnInitComplete
4. OnPreLoad
5. Page_Load
6. OnLoad
7. Button_Click
8. OnLoadComplete
9. OnPreRender
在A中重写OnPreRender方法代码:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Bind();
}
bind()中即是调用控件B中的方法的方法
-------------------------------分隔------------------------------------
by the way,记录一下调用用户控件方法的方法:
用户控件ID.方法名(),即infoControl.ShowCustomerInfo(, , , );
by the way,记录一下调用用户控件的方法:
<%@ Register Src="~/Controls/CustomInfoControl.ascx" TagName="ReadCustom" TagPrefix="ZDControl" %>
<ZDControl:ReadCustom ID="infoControl" ProductGuid="modifyCustomer" runat="Server" Visible="true" />