How to write a simple Web Custom Control Author: Robert Contact: Robert_luoqing@hotmail.com Web Custom Control function call sequence diagram (I call the process according to the sequence diagram homemade, there is something wrong place to look correct me): Note: 1, the sequence The function execution order in the area (1) of the figure is not determined. 2, the function within the area (2) is the function in the area when the subfeter is used for the first time, which is used to ensure that the sub-control has been initialized before the sub-control; in addition, must notes that CreateChildrenControls function during the call, if you do not control ChildrenControlsCreated variable, the system will ensure that only once (that is called multiple times EnsureChildControls function, CreateChildrenControls will only tune once, because when you call after the first, ChildrenControlsCreated is set to true, The CreateChildrenControls function will not be called later after EnsuneChildControls later. 3. Don't assign the ViewState in the SaveViewState function, because the system writes (serialized) to the value in the SaveViewState function, if the value is changed after this function, the value is Will not be serialized. 4. InamingContainer instructs the role of the interface: The above is not marked, which allows the sub-control created in CreateChildrenControls to maintain the postbackData value. For example: There are the following piece of code in CreateChildrenControls: TextBox m_text = new TextBox (); m_text.Text = "Test"; m_text.AutoPostBack = true; this.Controls.Add (m_text); base.CreateChildControls (); first The value in m_text is "test" when executed, when the user changes the value of the text box in the interface, if the custom web control implements the InamingContainer interface, the post back is still "SECORD" If the interface is not implemented, the value is "test" (I only pay attention to this difference, you can try it, find out other differences); 5, in addition to the Web Custom Control, in addition to loadViewState, SaveViewState these two places In addition to serialization and reverse sequencing using ViewState, other places are best used to use the non-public variables of the class, which is the benefits: 1, the code is clear (avoiding the viewstate and variables, sometimes it will make people confuse ); 2. Avoid multiple type conversions;
6. If you want to trigger an event in the Web Custom Control (Events raised by the non-child control), you must implement the iPostBackeVentHandler interface. The following code can explain how to raise an event: protected override void render (htmlTextWriter output {Output.write ("< A href = javascript: " this.page.getpostbackeventreference (this," first ") "> postback "); Base.Render (Output);} This code will enter a link to" Postback ", The content will be submitted to the server when clicked, and an event can be received in the RaisePostBackeVent (String Eventargument) function, where Eventargument is corresponding to the second parameter of Page.GetPostBackeventReference. Due to limited time, I hope everyone can experience themselves for other interfaces or features. The code in the following data guides is made as a reference. // ******************************************************** **************** / * * Requirements: * 1, record total number * 2, each page display record number * 3, current page * * The data parallelism : * 1, require the number of records, records, number of records, current pages * 2, requesting the previous page, the next page, the home page, the leading button, and give guidance event * 3, request Displays the display status of the guide button according to the current page. * 4, require customers to enter only the attribute once, and require the system to automatically maintain changes in each property.
* 5, a sequence of required control properties * * / using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; namespace CustomerTest {public delegate void DataBarClick (object e, int number ); ///
), DefaultValue (20)] public int number {get {return this.m_numberperPage;} set {if (value <1) throw new exception ("The number of records displayed per page cannot be less than 1"); this.m_numberPage = value ;}} protected override void CreateChildControls () {Table l_table = new Table (); // build the outer Table TableRow l_tbrow = new TableRow (); // only one line l_table.Rows.Add (l_tbrow); TableCell l_tbcell = new TableCell (); // create content l_tbrow.Controls.Add (l_tbcell) of the first column; l_tbcell.Wrap = false; LinkButtonFirst = new System.Web.UI.WebControls.LinkButton (); LinkButtonFirst.ID = "LinkButtonFirst" ; LinkButtonFirst.Text = "Home"; LinkButtonFirst.Click = new EventHandler (Guid_Click); l_tbcell.Controls.Add (LinkButtonFirst); l_tbcell = new TableCell (); // establish a "|" l_tbrow.Controls.Add (l_tbcell); l_tbcell.text = "|"; l_tbcell = new TableCell (); // Establish the contents of the second column l_tbrow.controls.add (l_tbcell); l_tbcell.wrap = false; linkButtonPreview = new system.web.ui.webControls. LinkButton (); linkButtonPreview.id = "LinkButtonPrevi ew "; LinkButtonPreview.Text =" Previous "; LinkButtonPreview.Click = new EventHandler (Guid_Click); l_tbcell.Controls.Add (LinkButtonPreview); l_tbcell = new TableCell (); // establish a" | "l_tbrow.Controls.Add (l_tbcell); l_tbcell.text = "|"; l_tbcell = new TableCell (); // Establish the content of the third column l_tbrow.controls.add (l_tbcell); l_tbcell.wrap = false; linkbuttonnext = new system.Web. Ui.WebControls.LinkButton (); linkButtonNext.id = "linkbuttonnext"; linkButtonNext.text = "Next"; linkbuttonnext.click = new eventhandler (guid_click);
l_tbcell.controls.add (linkbuttonnext); l_tbcell = new Tablecell (); // Established "|" l_tbrow.controls.add (l_tbcell); l_tbcell.text = "|"; l_tbcell = new Tablecell (); // Establish SUMMARY l_tbrow.Controls.Add (l_tbcell) four columns; l_tbcell.Wrap = false; LinkButtonLast = new System.Web.UI.WebControls.LinkButton (); LinkButtonLast.ID = "LinkButtonLast"; LinkButtonLast.Text = "End "; LinkButtonLast.Click = new EventHandler (Guid_Click); l_tbcell.Controls.Add (LinkButtonLast); this.Controls.Add (l_table); base.CreateChildControls ();} private void Guid_Click (object sender, System.EventArgs e) { SetGuide; if (this.Database.database.database.database.Database.database, this.m_currentpage);}} /// } If (((Control) sender) .ID == "LinkButtonLast") {this.m_CurrentPage = m_iTotalPage;}} this.LinkButtonFirst.Enabled = (this.m_CurrentPage == 1);! This.LinkButtonPreview.Enabled = (! this.m_CurrentPage == 1);! this.LinkButtonLast.Enabled = (this.m_CurrentPage == m_iTotalPage); this.LinkButtonNext.Enabled = (this.m_CurrentPage == m_iTotalPage);!} protected override void OnLoad (EventArgs e) { THIS.M_DESIGN = false; base.onload (e);} protected override void render (htmlTextWriter Writer) {if (this.m_design) Writer.write ("Data Pass Bar Control)); Base.Render (Writer); Protected Override Void LoadViewState (Object SavedState) {base.loadViewState (SavedState); / * * The value of the variable is used here, that is, the value of the * variable from ViewState. * * / This.m_CurrentPage = (int) this.ViewState [ "CurrentPage"]; this.m_NumberPerPage = (int) this.ViewState [ "NumberPerPage"]; this.m_TotalRecord = (int) this.ViewState [ "TotalRecord"] } Protected Override Object SaveViewState () {/ * * This is used here to sequence the variable to make PostBack to recover. * * / This.ViewState [ "CurrentPage"] = this.m_CurrentPage; this.ViewState [ "NumberPerPage"] = this.m_NumberPerPage; this.ViewState [ "TotalRecord"] = this.m_TotalRecord; return base.SaveViewState ();} Protected Override Void OnPrender (Eventargs E) {this.setguide (null); base.onprender (e);}}} // ******************************** *********************************************************** ********** Test code: Test.aspx <% @register tagprefix = "test" namespace = "customertest" assembly = "testdatabase"%> <% @ Page language = "c #" codebehind = "test.aspx.cs" autoeventwireup = "false" inherits = "testdatabase - // w3c // DTD HTML 4.0 Transitional //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////> En ">
HEAD>