Essential ASP. NET Reading Notes-webform

Source: Internet
Author: User
Client Server interaction through webform

Because HTTP connections are stateless, the page status cannot be saved through connections. Asp.net proposes a solution to save the current status of the page, the page with viewstate is used to store the final values of all page elements in the hidden 64-bit encoded string _ viewstate. The example is as follows:

In Asp.net, we are surprised to find that we can use the same event processing mechanism as winform. This is shown in the following example.

<!? Color. aspx? GT; <br/> <% @ page Language = "C #" %> </P> <p> <HTML> <br/> <SCRIPT runat = Server> <br/> protected void onred (Object SRC, eventargs e) <br/>{< br/> _ color. style ["background-color"] = "red"; <br/>}</P> <p> protected void ongreen (Object SRC, eventargs E) <br/>{< br/> _ color. style ["background-color"] = "green"; <br/>}</P> <p> protected void onblue (Object SRC, eventargs E) <br/>{< br/> _ color. style ["background-colo R "] =" blue "; <br/>}</P> <p> protected void page_init (Object SRC, eventargs E) <br/>{< br/> _ redbutton. serverclick + = new eventhandler (onred); <br/> _ greenbutton. serverclick + = new eventhandler (ongreen); <br/> _ bluebutton. serverclick + = new eventhandler (onblue); <br/>}</P> <p> protected void page_load (Object SRC, eventargs E) <br/>{< br/> If (! Ispostback) <br/>{< br/> _ color. style ["background-color"] = "red"; <br/> _ color. style ["width"] = "100"; <br/> _ color. style ["height"] = "100 "; <br/>}</P> <p> </SCRIPT> <br/> <body> <br/> <form runat = Server> </ p> <p> <H2> Asp. net color page </H2> <br/> <Div id = _ color runat = server/> <br/> <p> <br/> <input type = button id = _ redbutton value = "red" <br/> runat = server/> <br/> <input type = button id = _ greenbutton value = "green" <br/> runat = server/> <br/> <input type = button id = _ bluebutton value = "blue" <br/> runat = server/> <br/> </P> <br /> </form> <br/> </body> <br/> </ptml>

However, implementing such a mechanism requires a price. Asp.net has done a lot of work in the background.
the hidden field is also used, asp.net uses JavaScript to post events of different controls to the server in different States:

<HTML> <body> <br/> <form name = "_ ctl0" method = "Post" <br/> action = "color. aspx "id =" _ ctl0 "> <br/> <input type =" hidden "name =" _ eventtarget "value =" "/> <br/> <input type = "hidden" name = "_ eventargument" value = ""/> <br/> <input type = "hidden" name = "_ viewstate" value = "DD. .. = "/> </P> <p> <script language =" JavaScript "> <br/> <!? <Br/> function _ dopostback (eventtarget, eventargument) {<br/> var theform = document. _ ctl0; <br/> theform. _ eventtarget. value = eventtarget; <br/> theform. _ eventargument. value = eventargument; <br/> theform. submit (); <br/>}< br/> //? GT; <br/> </SCRIPT> </P> <p> <H2> Asp. net color page </H2> <br/> <Div id = "_ color" <br/> style = "background-color: red; width: 100; Height: 100; "/> <br/> <p> <br/> <input language =" JavaScript "<br/> onclick =" _ dopostback ('_ redbutton ','') "<br/> name =" _ redbutton "id =" _ redbutton "<br/> type =" button "value =" red "/> <br/> <input language = "JavaScript" <br/> onclick = "_ dopostback ('_ greenbutton ', '') "<br/> name =" _ greenbutton "id =" _ greenbutton "<br/> type =" button "value =" green "/> <br/> <input language = "JavaScript" <br/> onclick = "_ dopostback ('_ bluebutton ', '') "<br/> name =" _ bluebutton "id =" _ bluebutton "<br/> type =" button "value =" blue "/> <br/> </P> <br/> </form> <br/> </body> <br/> </ptml> <br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.