Asp.net dopostback

Source: Internet
Author: User

Asp.net has two methods for triggering callback

1. Using a button and imagebutton directly triggers a callback

2. Other controls_ Dopostback FunctionTrigger return

The principle is as follows:

1. a button or imagebutton directly triggers a callback. When an event is triggered on these two controls, the Control ID is transmitted as a parameter to the server so that the server can know which backend event the ing is.
1. default. aspx
<% @ Page title = "home page" Language = "C #" masterpagefile = "~ /Site. Master "autoeventwireup =" true"
Codebehind = "default. aspx. cs" inherits = "dopostback. _ default" %>

<Asp: Content ID = "headercontent" runat = "server" contentplaceholderid = "headcontent">
</ASP: content>
<Asp: Content ID = "bodycontent" runat = "server" contentplaceholderid = "maincontent">
<Asp: button id = "btnquery1" runat = "server" text = "query1"
Onclick = "btnquery_click"/>
<Br/>
<Asp: button id = "btnquery2" runat = "server" text = "query2"
Onclick = "btnquery2_click"/>

</ASP: content>

2. Generate htmlCode

<Form method = "Post" Action = "default. aspx" id = "ctl01">
<Div class = "aspnethidden">
<Input type = "hidden" name = "_ viewstate" id = "_ viewstate" value = "/comment ="/>
</Div>

<Div class = "aspnethidden">
<Input type = "hidden" name = "_ eventvalidation" id = "_ eventvalidation" value = "/wewawkvtzf9dwlgp + bidglhkitecimb/c9fsbmu0t/Hangzhou"/>
</Div>
<Input type = "Submit" name = "ctl00 $ maincontent $ btnquery1" value = "query1" id = "maincontent_btnquery1"/>
<Br/>
<Input type = "Submit" name = "ctl00 $ maincontent $ btnquery2" value = "query2" id = "maincontent_btnquery2"/>
</Form>

3. Click query1
Submit Form. You can obtain allkeys through request. Form, which has three keys.
1. _ viewstate
2. _ eventvalidation
3. ctl00 $ maincontent $ btnquery1
When the button control triggers PostBack, the ID of the button is used as a key of request. form. By enumerating the key value, you can find the control instance. If the control is a button control, it is determined that
This button control triggers an event

Ii. Use the _ dopostback JavaScript method to trigger the return

1. default2.aspx
<% @ Page title = "" Language = "C #" masterpagefile = "~ /Site. Master "autoeventwireup =" true "codebehind =" default2.aspx. cs "inherits =" dopostback. default2 "%>
<Asp: Content ID = "content1" contentplaceholderid = "headcontent" runat = "server">
</ASP: content>
<Asp: Content ID = "content2" contentplaceholderid = "maincontent" runat = "server">
<Asp: linkbutton id = "linkbutton1" runat = "server" text = "linkbutton1" onclick = "linkbutton#click"> </ASP: linkbutton>
</ASP: content>

2. html generated by defa2.2.aspx
The __eventtarget and _ eventargument hidden fields are automatically declared, pointing to the Control ID and corresponding additional parameters of the event.

(For example, if this control can trigger multiple events, additional parameters will indicate which event is triggered and cause the callback)

ASP. NET uses the values of these hidden domains to determine how to map client events to corresponding server events.

<Form method = "Post" Action = "default2.aspx" id = "ctl01">
<Div class = "aspnethidden">
<Input type = "hidden" name = "_ eventtarget" id = "_ eventtarget" value = ""/>
<Input type = "hidden" name = "_ eventargument" id = "_ eventargument" value = ""/>
<Input type = "hidden" name = "_ viewstate" id = "_ viewstate" value = "/Signature + daa5bzznqnz/w0ocuq/niwgiru8jdcz3ctec8 ="/>
</Div>

<SCRIPT type = "text/JavaScript">
// <! [CDATA [
VaR theform = Document. Forms ['ctl01'];
If (! Theform ){
Theform = Document. ctl01;
}
Function _ dopostback (eventtarget, eventargument ){
If (! Theform. onsubmit | (theform. onsubmit ()! = False )){
Theform. _ eventtarget. value = eventtarget;
Theform. _ eventargument. value = eventargument;
Theform. Submit ();
}
}
//]>
</SCRIPT>
<Div class = "aspnethidden">
<Input type = "hidden" name = "_ eventvalidation" id = "_ eventvalidation" value = "/examples/izuitxa/gzd45wth4pumtbi1_kvb"/>
</Div>
<A id = "maincontent_linkbutton1" href = "javascript :__ dopostback (& #39; ctl00 $ maincontent $ linkbutton1 & #39;, & #39; & #39 ;) "> linkbutton1 </a>
</Form>
</Body>
</Html>

Related Article

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.