简体   繁体   English

在ASP.NET页面生命周期与自定义服务器控件的问题和事件

[英]Issues with Custom Server Controls and Events in the ASP.NET page lifecycle

I am starting to go nuts with this one, it "seems" I can't get this to work in an elegant way and I'm sure i should be able to. 我开始对此很不满意,它“似乎”无法以一种优雅的方式工作,而且我确定我应该能够做到。

On the righthand side of the page I dynamically create Custom Server controls that have events in them that fire, I have proved this bit works in testing. 在页面的右侧,我动态创建了其中触发事件的Custom Server控件,我证明了这一点在测试中是可行的。

I have a Treeview on the left side of the page and this is autopopulated at page load assuming it is not a postback, if it is then its viewstate handles this nicely. 我在页面的左侧有一个Treeview,假设它不是回发页面,那么它将在页面加载时自动填充,如果是的话,则其viewstate可以很好地处理它。 When you click on one of the branches of the tree it will post back and when the event fires it will then populate the right side with new custom server controls that hold the inforation relevant to what branch you clicked. 当您单击树的其中一个分支时,它将回发,并且在事件触发时,它将在右侧填充新的自定义服务器控件,这些控件保存与您单击的分支有关的信息。 The right panel always populates with the correct controls, however the events don't fire when they are clicked as the new branch Id is only know after the page_load event (when the treeview event fires) which is too late for all the Event cleverness to register I think. 右侧面板始终使用正确的控件填充,但是单击事件时不会触发事件,因为仅在page_load事件(当树视图事件触发时)之后才知道新的分支ID,这对于所有事件的智能化来说都太晚了我想注册。

I have looked into using Response.Redirect, but then I lose the viewstate on the Treeview so have started looking at forcing a Javascript post back but haven't got very far with that. 我已经研究过使用Response.Redirect,但是随后我失去了Treeview上的viewstate,因此开始考虑强制返回Javascript,但还远远不够。

I really can't believe this is an unusual problem so hopeing there is an elegant solution. 我真的不敢相信这是一个不寻常的问题,因此希望有一个优雅的解决方案。 If its just i am going about it the total wrong way then happy to change all the logic if we can get this to work!! 如果只是我要解决的事情,那是完全错误的方法,那么如果我们能够使它起作用,那么很乐意更改所有逻辑!

Many many Thanks!! 非常感谢!!

Matt 马特

*UPDATE* * *更新* *

I'm not allowed to close this yet so will paste below 我尚未关闭此功能,因此将其粘贴在下面

seems typing that out got me thinking about the Javascript route and what .NET must have some where. 似乎输入这些内容使我开始思考Javascript路由以及.NET必须在何处存在。 A bit of reading and I have come up with this. 一些阅读,我想出了这一点。

PostBackOptions pbo = new PostBackOptions(pnlEvents); pbo.ActionUrl = Request.Url.AbsoluteUri; pbo.PerformValidation = false; string postback = ClientScript.GetPostBackEventReference(pbo); ClientScript.RegisterStartupScript(typeof(Page), "AutoPostBackScript", postback, true);

This will cause another postback if you run this after the event fires that you get your information from, allowing you to load the Server Controls forn Init or Load as long as you store your value in viewstate or its in another controls viewstate. 如果在事件触发后从您获取信息后运行此命令,则将导致另一次回发,只要您将值存储在viewstate或其另一个控件的viewstate中,就可以加载Server Controls forn Init或Load。

Events can be registered in Page_Init or Page_Load. 可以在Page_Init或Page_Load中注册事件。 If you are still lost, check ASP.NET Page Life Cycle Overview on MSDN . 如果仍然迷路,请查看MSDN上的ASP.NET页面生命周期概述

Update: You can simply send the param in URL, and get it in Page_Load, not using postback mechanism at all. 更新:您可以简单地在URL中发送参数,并在Page_Load中获取它,而不使用回发机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM