When I wrote an ASP program before, I knew that only a button with type set to submit can trigger a form to submit data to the server.
For example: Button in Asp.Net is equal to <input type="submit">.
But now many controls in Asp.Net can interact with the server side at will, such as LinkButton.
How is this achieved?
Is it a completely new way?
In fact, this is just a workaround from Microsoft.
Let’s first take a look at the client code.
Here is a page with LinkButton,
In fact, LinkButton on the client side is equivalent to A in html.
Let’s take a look at why LinkButton can also interact with the server?
When we right-click to view the source code of the page, we see:
<script type="text/javascript">
<!--
var theForm = document.forms['ctl00'];
if (!theForm) {
theForm = document.ctl00;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
// -->
</script>
<a id="simpleLinkButton1" href="javascript:__doPostBack('simpleLinkButton1','')">Click Me</a>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
From the above code, we can see that simpleLinkButton1 is a link, and the form is submitted through the client code.
Two of the hidden fields are used to exchange data by assigning the two parameter values of __doPostBack to these two hidden fields.
This is the most intuitive reason why LinkButton also has the ability to submit data.
So how was the code above generated?
We can clearly know by looking at the source code of LinkButton:
protected internal override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if ((this.Page != null) && this.Enabled)
{
this.Page.RegisterPostBackScript();
if ((this.CausesValidation && (this.Page.GetValidators(this.ValidationGroup).Count > 0)) || !string.IsNullOrEmpty(this.PostBackUrl))
{
this.Page.RegisterWebFormsScript();
}
}
}
The above is just a record of some of my experiences during the study and work process to prevent myself from forgetting.
I hope to communicate more with you all!
http://www.cnblogs.com/maplye/archive/2006/08/29/489338.html