简体   繁体   English

动态创建控件并在回发中保存控件值 - ASP.Net C#

[英]Dynamically Create controls and save the controls values across postback - ASP.Net C#

Consider this - 考虑一下 -

I allow the end user to create a tab control dynamically in an ASP.Net page by getting some details. 我允许最终用户通过获取一些细节在ASP.Net页面中动态创建选项卡控件。 For each and every tab added I get some settings - 对于添加的每个标签,我都会得到一些设置 -

  1. Tab Title 标签标题
  2. Tab Content 标签内容
  3. Tab Footer link 标签页脚链接

So I get these details for each and every tab. 所以我得到每个标签的这些细节。 The user is provided with a button 'Add Tab' to more one more tab. 用户向另外一个选项卡提供了一个“添加标签”按钮。 So I need to add one more tab settings panel in the page to get the tab settings. 因此,我需要在页面中添加一个标签设置面板以获取标签设置。 But doing so, I lose the values entered in the previously created dynamic tab settings panel. 但是这样做,我丢失了先前创建的动态标签设置面板中输入的值。 The user can add as many tabs and enter settings for each and every tab. 用户可以添加任意数量的选项卡,并为每个选项卡输入设置。 Finally when they save it I build the tab control (based on their settings and content) and I render the control. 最后,当他们保存它时,我构建了tab控件(基于他们的设置和内容),然后我渲染控件。

Since the controls are dynamic, I'm able to thing of two options - 由于控件是动态的,我可以选择两种方式 -

  1. Recreate previously created n - 1 tab settings panel while adding a nth tab. 添加第n个选项卡时,重新创建先前创建的n - 1选项卡设置面板。 So across each and every postback I need to recreate the settings panel for each and every tab. 因此,在每个回发中,我需要为每个选项卡重新创建设置面板。 But I don't know how to persist the values in this case. 但我不知道如何坚持这种情况下的价值观。
  2. Add the settings panel in the client side with simple html controls and on page UnLoad parse the controls -> Parse the settings -> Save it in a hidden field -> Get it in the server side. 使用简单的html控件在客户端添加设置面板,并在页面上UnLoad解析控件 - >解析设置 - >将其保存在隐藏字段中 - >在服务器端获取它。 In this case I lose all the server side capabilities where I need to write validation scripts for the controls. 在这种情况下,我失去了所有服务器端功能,我需要为控件编写验证脚本。

Any insights on how better to do this? 有关如何更好地做到这一点的任何见解?

I would add the controls server-side, just remember that you need to re-create all controls on every postback 我会添加控件服务器端,只需记住你需要在每次回发时重新创建所有控件

You could save your details of any controls that need to be created in viewstate then build your control tree from that in CreateChildControls 您可以保存需要在viewstate中创建的任何控件的详细信息,然后从CreateChildControls中构建控件树

One method is to save data that you need to recreate the tabs in the ViewState. 一种方法是保存在ViewState中重新创建选项卡所需的数据。 You can do this during the PreRender event for example. 例如,您可以在PreRender事件期间执行此操作。 On Postback you need to recreate the controls you had originally, maintaining their original IDs and their order in the hierarchy. 在回发时,您需要重新创建最初的控件,在层次结构中维护其原始ID及其顺序。 You can do this during the LoadViewState phase. 您可以在LoadViewState阶段执行此操作。

Once in Page_Load you can create your new tab 进入Page_Load后,您可以创建新标签

I've implemented option #1 in the past. 我过去实现了#1选项。 You'll have to recreate your controls tree at a certain point in page events lifecicle, so that when view event to restore viewstate kicks in, it has all the controls it needs. 您必须在页面事件生命周期中的某个点重新创建控件树,以便在恢复视图状态的视图事件启动时,它具有所需的所有控件。 As far as I remember, you also need to restore all the controls in exactly same hierarchy ans with identical names it existed when page was rendered and view stated persisted, prior to be sent to the client. 据我所知,您还需要恢复完全相同的层次结构中的所有控件,并且在将页面呈现之前存在相同的名称并且在发送到客户端之前保持视图状态。 If there's any discrepancy, loading viewstate will not work for dynamic controls. 如果存在任何差异,则加载viewstate将不适用于动态控件。

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

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