Implement event and automatic save value in a web custom control

xiaoxiao2021-03-06  41

using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.Collections.Specialized; namespace MyWebControls {///

/// to create a derived class WEBCONTROL // / Implement a public constructor, which calls the base class constructor to specify the server control to output an input element // / to rewrite the ADDATTRIBUTESTOREnder method, call this method to allow derived classes to root element input Add Properties /// We will add a Name property that is derived from UNIQUEID feature, and ASP.NET uses this feature to store the unique ID of each control. /// [DefaultProperty ("Text"), ToolboxData ("<{0}: mytextbox runat = server> ")] public class mytextbox: system.Web.ui.WebControls .WebControl, IpostBackDataHandler {public mytextbox (): base ("input") {}

// Save the value using the ViewState object, and the valid range of this object can be accessed for the current page. Finally saved on the client. Each time you will carry // viewState is a StateBag class, the stored data type has int Bool string or array and other basic data types, and ArrayList, HashTable, // or type of type converters, you can serialize Public string text {get {if (ViewState ["Value"] == null) {return string.empty;} return (string) ViewState ["value"];} set {viewState ["value"] = value;}

protected override void AddAttributesToRender (HtmlTextWriter writer) {base.AddAttributesToRender (writer); writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID); writer.AddAttribute ( "type", "text"); if (! Text = null) writer.AddAttribute ("Value", Text);

#Region iPostBackDataHandler member

/ / In order to access the return data, the server control should implement the iPostBackDataHandler interface. There are two ways public void raisePostDataChangeDevent () {// If the user returns the data changes, event IF (ONMYTEXTCHNAGED! = Null) {ONMYTEXTHNAGED (this, Eventargs .EMPTY);}}

// This method is called when there is a loopback and a control is returned, which is called for all the controls that need to access the feedback data on the page. / / This method If returned, then RaisePostDataChangeDMethod will be called after all other controls with return data on the page are called. // If it returns a holiday, it is not called. Since the incident causes an incident in this method, it is necessary to raise an event in RaisePostDataChangeDevent. // public boolLoAdPostData (String PostDataKey, NameValuey, NameValuech = false; // Do you want to trigger an email // If the last text is different from the back text is different (Text! = Postcollection [postDataKey]) { RaiseEvent = true; text = postcollection [postDataKey]; // Save the repelted value} Return RaiseEvent;} #ENDREGION

// Register an event, text change event public evenetaged;

}

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

New Post(0)