[ASP.NET Tutorial] II. Web Forms

xiaoxiao2021-03-06  22

1. Server processing

Although form.html can be raised by POST or GET method, it still cannot handle user input, still a static page, now we need to process user input and return in the server.

There are many ways to handle user input on the server side, because this article is an ASP.NET tutorial, so just about ASP.NET.

ASP.NET uses Web Form to describe the web page, with object-oriented programming methods, with .NET Framework, make web application design easier, more efficient, and more powerful.

2. Web Forms

Start with a simple sample program:

Doadd.aspx

HTML code:

Web Form </ Title></p> <p></ hEAD></p> <p><body></p> <p><form runat = "server"></p> <p><asp: textbox runat = "server" id = "value1" /></p> <p> <asp: textbox runat = "server" id = "value2" /></p> <p><asp: button runat = "server" text = "=" onclick = "add" /></p> <p><asp: label runat = "server" id = "result" /></p> <p></ form></p> <p></ body></p> <p></ html></p> <p><script language = "c #" runat = "server"></p> <p>Void Add (Object Sender, Eventargs E)</p> <p>{</p> <p>IF (value1.text == "")</p> <p>Value1.text = "0";</p> <p>IF (value2.text == ")")</p> <p>Value2.text = "0";</p> <p>Try</p> <p>{</p> <p>Result.text = (int.Parse (value1.text) int.parse (Value2.Text)). TOSTRING ();</p> <p>}</p> <p>Catch</p> <p>{</p> <p>Result.text = "Error";</p> <p>}</p> <p>}</p> <p></ script></p> <p>Here, it is assumed that the reader has properly installed IIS and ASP.NET, and the home directory of the web server is C: / INETPUB / WWWWROOT /, there is no special instructions in the future.</p> <p>Place this file in C: / INETPUB / WWWROOT /, save it as doadd.aspx, then enter http://localhost/doadd.aspx at the address bar of the browser, the web form is rendered in the browser.</p> <p>There are 2 input boxes on the page, one button, when the user clicks the "=" button, the program adds the value in the 2 input boxes, then display the result after the equal number button, if the error is entered The result shows "error".</p> <p>This program is very simple, but it is a complete dynamic web program. It handles user input on the server side and then dynamically generates an HTML page.</p> <p>After using IE to open the page, select the menu [View] | [Source File], you will see the following HTML source code:</p> <p>HTML code:</p> <p><html></p> <p><HEAD></p> <p><title> Web Form </ Title></p> <p></ hEAD></p> <p><body></p> <p><form name = "_ CTL0" Method = "post" action = "1.aspx" id = "_ ctl0"></p> <p><input type = "hidden" name = "__ viewstate" value = "ddwxote0ndy4ode2ozs yeczcnblhd6d7s7 uue writer / 6i =" /></p> <p><input name = "value1" type = "text" id = "value1" /></p> <p> <input name = "value2" TYPE = "text" id = "value2" /></p> <p><Input Type = "Submit" Name = "_ CTL1" value = "=" /></p> <p><span id = "result"> </ span></p> <p></ form></p> <p></ body></p> <p><html></p> <p>It can be seen that the TEXTBOX control becomes <input type = "text" /> tag, the Button control becomes <input type = "submit" /> tag, the Label control has changed <span> </ span> tag, which is also There is a strange <input type = "hidden"> tag, named __viewstate, now only need to know that it is an ASP.NET to handle a mechanism for climate-trip data between servers and browsers, will be described in detail later.</p> <p>Observe the ASPX file, you can see that all the controls and all the controls have an attribute: Runat = "Server", which means that this tag is running on the server, not directly to the client browser.</p> <p>One <asp: ...> control is actually corresponding to classes in system.web.ui.webcontrols, such as <asp: textbox /> corresponds to System.Web.ui.WebControls.TextBox, <ask: Button / > Corresponds to System.Web.ui.WebControls.Button, these tags can also contain some other properties, such as the text attribute of <asp: button /> corresponding to the text attribute in the System.Web.ui.WebControls.Button class, The more special is the ID attribute, which indicates the programmable name of the control, which is used to access the control in the code.</p> <p><script> </ script> script block is also tagged as runat = "server" because this is the code running on the server, not the client script.</p> <p>Observing <asp: button /> tag, there is an attribute: onclick = "add", which specifies an event handler, triggering the Click event when the user clicks the button, then program call <script> </ script> Add method in block. The rules that add event processing are: add the event name to the prefix "ON" as an attribute of the control tag. For example: <asp: button online = "button_load" runat = "server" />, where the event name can be found in the .NET Framework SDK documentation. Next, the underlying principle of the event model is briefly described. In the HTML of the server, there is an HTML Form, where there is a commit button, when you click this button, the browser uses the POST method to pass the input of the <input> control. Back to the server, ASP.NET knows that the user clicks on the "=" button, and responds to the Click event in the server side, then the ASP.NET calls the add method, and then generates an HTML page once, because when calling the add method, Label The TEXT attribute of the control is assigned, so the <span> </ span> tag in the generated HTML contains a string, corresponding to the text attribute value of the Label control.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-39668.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="39668" 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.037</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 = 'zC12Wa_2B_2Bufg4mQKlzccDjC_2BLoCQQc4PLhtjlCM7tOunZLDLlZUAuRN2ou73MF2VhcU45QIcIpykKhT3xR6JrOQ_3D_3D'; 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>