How to write a simple web custom copy

zhaozj2021-02-11  196

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 ); ///

///// [defaultproperty ("totalRecord"), ToolboxData ("<{0}: databar NumberPage = 20 TotalRecord = 0 CURRENTPAGE = 1 runat = server> ")] public class DataBar: System.Web.UI.WebControls.WebControl, INamingContainer {protected System.Web.UI.WebControls.LinkButton LinkButtonPreview; protected System.Web.UI.WebControls .LinkButton LinkButtonNext; protected System.Web.UI.WebControls.LinkButton LinkButtonLast; protected System.Web.UI.WebControls.LinkButton LinkButtonFirst; private bool m_Design = true; protected int m_TotalRecord; // record the total number of protected int m_CurrentPage = 1; / / Currently displayed page number protected; // Each page Display record number // Establish an event in the number of job data pages PUBLIC Event DataBarclick Database (/// /// GET or SET data total record number / // [Bindable RUE), Category ("DATA"), DefaultValue (0)] public int totalRecord {get {returnim_totalRecord;} set {if (value <0) throw new exception ("The total number of records given is less than 0 "); This.m_totalRecord = value;}} /// /// GET or SET Currently displayed page number /// [Bindable (True), Category (" Data "), DefaultValue 1)] Public int currentpage {get {returnim_currentpage;} set {i (value <1) throw new exception ("The current page given less than 1"); this.m_currentpage = value;}} // / /// GET or SET: record number ///// / summary> [Bindable (TRUE), Category ("DATA"

), 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);}} ///

/// Used to excite DataBarClick event /// public Void ondatabase (this.Database.database.database.database.Database.database);}} /// /// This is used to display each element in the guiding bar Status /// /// Indicates the button of the one of the guiding column elements, if not in touch by the guide, then the null value private void setGuide (object sender) {int m_iTotalPage = Convert.ToInt32 (System.Math.Ceiling (Convert.ToDouble (this.m_TotalRecord) /Convert.ToDouble (m_NumberPerPage))); if (m_iTotalPage

} 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 ">

test </ title></p> <p><meta name = "generator" content = "Microsoft Visual Studio .NET 7.1"></p> <p><meta name = "code_language" content = "c #"></p> <p><meta name = "vs_defaultclientscript" content = "javascript"></p> <p><meta name = "vs_targetschema" content = "http://schemas.microsoft.com/intellisense/ie5"></p> <p></ HEAD></p> <p><body ms_positioning = "gridLayout"></p> <p><form id = "form1" method = "post" runat = "server"></p> <p><Test: DataBar Runat = "Server" id = "stdbar"> </ test: databar></p> <p><Font face = "Song"> </ font></p> <p></ form></p> <p></ body></p> <p></ Html></p> <p>Test.aspx.cs</p> <p>using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls Using system.Web.ui.htmlcontrols;</p> <p>namespace TestDataBar {/// <summary> /// Summary description for Test /// </ summary> public class Test: System.Web.UI.Page {protected CustomerTest.DataBar stDBar; private void Page_Load (object sender, System. . Eventargs e) {if (! This.ispostback) {this.stdbar.totalRecord = 100; this.stdbar.NumberPage = 15; this.stdbar.currentpage = 1;}}</p> <p>#Region Web Form Designer generated code override protected void oninit (Eventargs e) {//// Codegen: this call is required by the asp.net web form designer. // initializecomponent (); base.onit (e);} / // <summary> /// Required method for Designer support -. do not modify /// the contents of this method with the code editor /// </ summary> private void InitializeComponent () {this.stDBar.databarClick = New customertest.database; this.load = new system.eventhandler (this.page_load);} #ENDREGION</p> <p>Private void stdbar_database {this.response.write ("Current page is: No. Number.tostring () " <br> "); this.Response.write (" total record number is: " this.stdbar.totalRecord.toString () " <br> "); this.Response.write (" The number per page is: " this.stdbar.NumberPage.toString () " <br> ") }}}</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-5702.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="5702" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.046</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'viwJLtDG6KPA3zXZ4LTxaJGdlBA6ZkPNUy9q_2BgTykTJKi_2B3qZUZNuG11p0vQY4eWmanLe_2BwAgABH2ybN'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>