我可以调用用户控件的Page

编程入门 行业动态 更新时间:2024-10-28 08:16:31
本文介绍了我可以调用用户控件的Page_Load事件aspx页面的Page_Load事件之前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

问题是直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

本文发布于:2023-11-10 15:36:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575763.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   用户   Page

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!