How to dynamically load web control remember view status after Postback (ViewState)

xiaoxiao2021-03-06  91

When we design the ASP.NET web program, the Web Contral on the form is not designed. Instead, it will be added at runtime. However, this brings a problem, which is the Web Contarel because it is added after SaveViewState, so it is not saved, all written data after Poskback will be lost. How to solve the above problem. I wrote a container control that is a web contorl, which is inherited by Panel.

Mainly to override the form of SaveViewState and LoadViewState:

Using system.web.ui.design; use system.web.ui.design.webcontrols; using system.web.ui.webcontrols; using system.ComponentModel; using system.xml;

namespace DBauer.Web.UI.WebControls {[Designer ( "System.Web.UI.Design.ControlDesigner"), ToolboxData ( "<{0}: DynamicControlsPanel runat = server> ")]

Public Class DynamicControlSpanel: Panel {#Region Customized Properties

[DefaultValue (HandleDynamicControls.DontPersist)] public HandleDynamicControls ControlsWithoutIDs {get {if (ViewState [ "ControlsWithoutIDs"] == null) return HandleDynamicControls.DontPersist; else return (HandleDynamicControls) ViewState [ "ControlsWithoutIDs"];} set {ViewState [ "ControlsWithoutIDs "] = Value;}}}

#ndregion custom properties

#Region View Status Management Protected Override Void LoadViewState (Object SavedState) {Object [] ViewState = (Object []) SavedState;

Pair persistinfo = (pair) ViewState [0]; Foreach (Pair Pair in (arraylist) Persistinfo.second) {RestoreChildstructure (Pair, this);}

Base.LoadViewState (ViewState [1]);

Protected Override Object SaveViewState () {if (httpcontext.current == null) Return NULL;

Object [] viewState = new object [2]; viewState [0] = persistchildstructure (this, "c"); viewstate [1] = base.saveViewState (); return viewState;}

Private Void Restorechildstructure (Pair Persistinfo, Control Parent) {Control Control;

String [] persistedstring = persistinfo.first.toString (). split (';');

String [] Typename = persistedString [1] .split (':'); switch (TypenAme [0]) {CASE "C": type type = type.gettype (Typename [1], True, True; Try {Control = (Control) Activator.createInstance (TYPE);} catch (exception e) {throw new argumentException (String.Format ("Type '{0}' Can't Recovery", Type.

Tostring ()), e);} Break; default: throw new argumentException ("Unrecognizable type. Cannot recover status.");}

Control.id = persistedstring [2];

Switch (persistedString [0]) {CASE "C": parent.controls.add (control); Break;}

Foreach (Pair Pair in (arraylist) persistinfo.second {restorechildstructure (Pair, Control);}}

Private Pair Persistchildstructure (Control Control, String ControlCollectionName) {String TypeName; arraylist childpersistinfo = new arraylist ();

if (control.ID == null) {if (ControlsWithoutIDs == HandleDynamicControls.ThrowException) throw new NotSupportedException ( "You must set your ID"); else if (ControlsWithoutIDs == HandleDynamicControls.DontPersist) return null;}

TypenAme = "C:" Control.gettype (). AssemblyqualifiedName

String persistedString = controlcollectionname ";" Typename ";" Control.ID;

if (!! (control is UserControl) && (control is CheckBoxList)) {for (int counter = 0; counter

#Region Heggance Style Protected Override Void Render (System.Web.ui.htmlTextWriter Writer) {base.render (Writer);} // #endregion reconciliation control style}

Public Enum HandledynamicControls {DontPersist, Persist, ThrowException}}

I hope to give a good helper with friends in this area. At the same time, you will also welcome everyone to rewrite better.

转载请注明原文地址:https://www.9cbs.com/read-123892.html

New Post(0)