问题是直forward.Can我叫用户控件的Page_Load中aspx页面的Page_Load中过吗?
The question is straight forward.Can I call page_load of usercontrol before page_load of aspx page?
修改的
EDIT
的它无法调用父母的Page_Load之前,孩子的Page_Load事件。的
我有这种所谓的just4test测试puropse的aspx类。它的ASPX code是:
I have this aspx class called just4test for testing puropse. Its aspx code is:
<body> <form id="form1" runat="server"> <uc1:lcont ID="lcont1" runat="server" /> <div> </div> </form>
你可以它使用的是用户控件文件中看到。
As you can see it is using a usercontrol file.
和其code后面的是:
And its code behind is :
protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack) { lcont1.my_tv_SelectedNodeChanged(sender, e); } }现在的用户控件是:
public partial class lcont : System.Web.UI.UserControl { sql_con sq = new sql_con(); protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { bind_data(); } } public void mytry() { Console.WriteLine("helo"); } public void bind_data() { main_cat mcobj = new main_cat(); List<main_cat> mclist = new List<main_cat>(); mclist = mcobj.get_mcatlist(); populatetree(mclist); } public void populatetree(List<main_cat> mytnode) { TreeNode parentnode = null; foreach (main_cat mc in mytnode) { parentnode = new TreeNode(mc.Cname, mc.Cid.ToString()); foreach (sub_cat sc in mc.scl) { TreeNode cnode = new TreeNode(sc.PRODUCT, sc.SID.ToString()); parentnode.ChildNodes.Add(cnode); } parentnode.Collapse(); my_tv.Nodes.Add(parentnode); } } public void my_tv_SelectedNodeChanged(object sender, EventArgs e) { TreeView tv = sender as TreeView; //TreeView tv = new TreeView(); var selectedN = tv.SelectedNode; if (selectedN.Parent != null) { var id = tv.SelectedNode.Value; var name = tv.SelectedNode.Text; //Session["mySvar"] = id; ScriptManager.RegisterStartupScript(this, this.GetType(), "nething", "show("+id+")", true); } else { //Response.Redirect("test.aspx"); } }}
我需要调用的函数my_tv_SelectedNodeChanged(发件人,E); just4test页面的Page_Load之前用户控件和我做so.But当我运行它表示,该计划
I need to call the function my_tv_SelectedNodeChanged(sender, e); of usercontrol before the page_load of just4test page and i have done so.But when i run the program it says
对象引用未设置到对象的实例。
Object reference not set to an instance of an object.
我应该怎么办?
推荐答案Page_Load中没有,但之前的OnInit occures。取决于你的实际问题,你可以有你的移动逻辑。
Page_Load no, but OnInit occures before. Depends on your actual problem, you can move your logic there.
或者呼叫的用户控制的公共方法您在父页Page_Load事件做任何事情之前
Or call a public method of the User control before you do anything else in the Parent Page Page_Load event
请参阅页生命周期
更多推荐
我可以调用用户控件的Page
发布评论