简体   繁体   English

基于在另一个用户控件中的选择来重新创建用户控件中的动态控件

[英]recreation of dynamic controls in user control based in selection in another user control

About 5 months ago I was tasked with creating a new intranet site for my current employer as the old one is a nightmare to work with. 大约5个月前,我的任务是为我现在的雇主创建一个新的内部网站,因为旧的网站是一个噩梦。 The site uses multiple .NET languages ( classic asp , VB , and C# ) with multiple .NET frameworks (1.0, 2.0, 3.5, few places with 4.0). 该站点使用多种.NET语言( classic aspVBC# )和多个.NET frameworks (1.0,2.0,3.5,很少有4.0的地方)。 Simple changes that should only take an hour to implement and test would take days just to implement. 只需要花费一个小时来实现和测试的简单更改只需要几天才能实现。

The new intranet sites content is controlled through user controls that are loaded dynamically at load time based on the page you are on and the access level you have. 新的Intranet站点内容通过用户控件进行控制,这些控件在加载时根据您所在的页面和您拥有的访问级别动态加载。 Each user control has a specific task and does not affect any other user control on the page. 每个用户控件都有一个特定的任务,不会影响页面上的任何其他用户控件。

About 3 weeks ago my database guy (was an application developer at his last job) pitched this idea to the middle management that the user controls could talk to each other and affect the selections available in each user control (all this without my knowledge). 大约3个星期前,我的数据库人(在他上一份工作中是一名应用程序开发人员)向中间管理人员提出这个想法,即用户控制可以相互通信并影响每个用户控件中的可用选择(所有这些都是我不知情的)。

At first, I didn't think it was possible when I heard. 起初,当我听到时,我认为这是不可能的。 Then, everything I read about having user controls communicate with each other indicated that the user controls had to know about each other and that wasn't possible since all of the user controls are load at runtime based off of the access level you have. 然后,我读到的关于让用户控件彼此通信的所有内容都表明用户控件必须彼此了解并且这是不可能的,因为所有用户控件都是基于您拥有的访问级别在运行时加载的。 I found a solution last week were I could have a user control fire a custom event handler and have my other user controls listen for that specific custom event handler. 我上周找到了一个解决方案,我可以让用户控件触发自定义事件处理程序,并让我的其他用户控件侦听该特定的自定义事件处理程序。

Now, today, I was asked if I could add filtering to the contact management part of the site that lists all of our clients similar to how Ebay has there filters on the left that allows you to drill down farther into the results returned. 现在,今天,我被问到是否可以向网站的联系人管理部分添加过滤,其中列出了我们的所有客户端,类似于Ebay左侧的过滤器,可以让您深入查看返回的结果。 For example, you search "flat screen tvs". 例如,您搜索“纯平电视”。 Ebay will list all results that match you search and on the left you can select the size range or the brands to narrow down the results. 易趣将列出与您搜索匹配的所有结果,在左侧您可以选择尺寸范围或品牌以缩小搜索范围。

On the page I setup I load 3 user controls to handle the criteria and the results. 在我设置的页面上,我加载了3个用户控件来处理标准和结果。 Control1 has all of the basic search criteria (ex. industry, region state, ect), control2 has the filters for drilling down the results from control1 . Control1具有所有基本搜索条件(例如行业,区域状态等), control2具有用于向下钻取control1结果的过滤器。 Control3 displays the clients based off the criteria in control1 (so control1 fires an event that control2 and control3 hear and they both display the results based on control1). Control3显示客户端的基于关断CONTROL1的标准(所以CONTROL1火灾的事件control2control3听到并且它们都显示基于CONTROL1的结果)。 Now I select the criteria from control2 and fire the event that control3 can hear and displays the results. 现在,我选择的标准control2 ,并触发该事件control3能听到并显示结果。

All of this works, the problem I am having is that the controls in control2 are built dynamically and when the event in control1 is fired -> then control2 posts back to fire the event for control3 to hear I lose all the dynamic controls in control2 as the controls can't be recreated in the Page_Init because the values passed in from the custom event in control1 no longer exists because control2 did the postback and the event from control1 is only fired when control1 postsback. 所有这一切工作,我遇到的问题是,在控制control2动态编译并当在事件control1被解雇- >然后control2回发火灾的事件control3听到我失去所有的动态控件control2作为无法在Page_Init重新创建控件,因为从control1中的自定义事件传入的值不再存在,因为control2了回发,而control1的事件仅在control1回发时触发。 What is the best way to store the values passed in to control2 from control1 's custom event or get control1 to repass the values when control2 posts-back so I can recreate the dynamic controls in control2 ? 什么是存储在传递的值的最佳方式control2control1的自定义事件或得到control1时,再通过数值control2帖回,所以我可以重新创建动态控件control2

Note: I tried using sessions but had trouble reassigning values from control1 after the first search. 注意:我尝试使用会话但在第一次搜索后无法从control1重新分配值。 I believe the reason they don't work is due to the way I have control1 setup and the creation of dynamic controls in control2 is skipping over getting the session values. 我相信他们不工作的原因是因为我有控制1设置,在创建动态控件的方式control2被跳过获取会话值。

I thank all of you in advance for your (hopefully) helpful responses. 我提前感谢你们(希望)有用的回复。

Update 更新
Turns out that the way I was loading my usercontrols at runtime on my default page is the reason why the dynamic control in 'control2' were not being recreated when 'control2' posted back. 事实证明,我在默认页面上运行时加载我的usercontrols的方式是'control2'回发时没有重新创建'control2'中的动态控件的原因。 My default page loaded the usercontrols in the 'page_load' instead of the 'page_init' (must have forgot to move the loading of usercontrol to the 'page_init' like all my other pages). 我的默认页面在'page_load'而不是'page_init'中加载了usercontrols(必须忘记将usercontrol的加载移动到'page_init',就像我的所有其他页面一样)。 Made the switch and the dynamic controls are recreated on postback. 制作开关并在回发时重新创建动态控件。

The only issue that I had after moving my code from the 'page_load' to the 'page_init' was that the 'checkbox' controls would be unchecked on postback even though I checked them. 将我的代码从'page_load'移到'page_init'后,我唯一的问题是,即使我检查了它们,也会在回发时取消选中'复选框'控件。 I was able to over come this with a few session variables. 我能用一些会话变量来解决这个问题。

This is a common problem. 这是一个常见问题。

Only controls dynamically created in your page_init event can survive a postback. 只有在page_init事件中动态创建的控件才能在回发中存活。

During page_init , dynamically created controls become part of the DOM , and thereby have sessionstates . page_init期间,动态创建的控件成为DOM一部分,从而具有sessionstates If you can re-factor your code to fire the dynamic control creation during page_init , your controls should survive. 如果您可以重新设置代码以在page_init期间page_init动态控件创建, page_init您的控件应该能够存活。

Update: 更新:

I realize from your comments and post that you're reluctant to use Sessions. 我从你的评论和帖子中了解到你不愿意使用Sessions。 Problem is that Sessions are the ONLY way to save your controls. 问题是Sessions是保存控件的唯一方法。

  • One way I dealt with this case was to create a Class Object with Lists of Controls. 我处理此案例的一种方法是使用控件列表创建一个类对象。 When I came back to the page, if the Object existed I used it as a default. 当我回到页面时,如果Object存在,我将其用作默认值。
  • Second way I approached this was to save the search criteria in Session and feed the criteria to my dynamic control creation method. 我接下来的第二种方法是在Session中保存搜索条件,并将条件提供给我的动态控件创建方法。

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

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