_ INVALID doPostBack Processing Method

Source: Internet
Author: User

Sometimes we want to write _ doPostBack ("id", "") when the client triggers the CLICK Event of the server-side control, but why does it expire sometimes?
By viewing the client source code, we found that the JS section _ doPostBack () was not generated on the page.
Originally, ASP. NET had to generate _ doPostBack on the premise that the server control had AutoPostBack = true and the control had to be enabled.

What should we do if we do not have the above conditions but want the page to generate _ doPostBack?
Solution:
The original method in the Page is GetPostBackEventReference, which can generate _ doPostBack, which has two versions.

1. Obtain the test using the sending instruction writing function. (This test will) Let the server go back to the website when calling.
Public string GetPostBackEventReference (Control );

2. Obtain the test using the sending instruction writing function. (This test will) Let the server go back to the website when calling. At the same time, this method also allows the server to control the operation on the server. Public string GetPostBackEventReference (Control, string );

If you only want to generate _ doPostBack, you can use the first one,
The second type with parameters can be used to perform different actions by determining the included parameters,
The following is an example.

Front-end page
There is a server control

<Asp: Button id = "Button1" runat = "server" Text = "Button"> </asp: Button>

A client control is used to trigger the server

<A href = "#" onclick = "document. getElementById ('button1'). click ()"> trigger a server button event </a>

(2)
Use GetPostBackEventReference to generate _ doPostBack () for the client ()
Front-end

<A href = "#" onclick = "<% = PostBack () %>"> trigger server button events </a>

Background

Protected string PostBack ()
{
Return this. Page. GetPostBackEventReference (this. Button1, "haha ");
}

You can use _ EVENTARGUMENT = "haha" to determine whether the PostBack of the link is clicked.
Write the button event of Button1 as follows:

If (Request ["_ EVENTARGUMENT"] = "haha ")
{
Response. Write ("this is the linked PostBack ");
}
Else
{
Response. Write ("this is not the linked PostBack ");
}

The following is an example on Microsoft's MSDN. It looks good.

Public class MyControl: Control, IPostBackEventHandler
{

// Create an integer property that is displayed when
// The page that contains this control is requested
// And save it to the control's ViewState property.
Public int Number
{
Get
{
If (ViewState ["Number"]! = Null)
Return (int) ViewState ["Number"];
Return 50;
}

Set
{
ViewState ["Number"] = value;
}
}

// Implement the RaisePostBackEvent method from
// IPostBackEventHandler interface. If 'inc' is passed
// To this method, it increases the Number property by one.
// If 'dec 'is passed to this method, it decreases
// Number property by one.
Public void RaisePostBackEvent (string eventArgument)
{
If (eventArgument = "inc ")
Number = Number + 1;

If (eventArgument = "dec ")
Number = Number-1;
}


[System. Security. Permissions. PermissionSet (System. Security. Permissions. SecurityAction. Demand, Name = "FullTrust")]
Protected override void Render (HtmlTextWriter writer)
{
// Converts the Number property to a string and
// Writes it to the containing page.
Writer. Write ("The Number is" + Number. ToString () + "(");

// Uses the GetPostBackEventReference method to pass
// 'Inc' to the RaisePostBackEvent method when the link
// This code creates is clicked.
Writer. Write ("<a href = \" javascript: "+ Page. GetPostBackEventReference (this," inc ") +" \ "> Increase Number </a> ");

Writer. Write ("or ");

// Uses the GetPostBackEventReference method to pass
// 'Dec 'to the RaisePostBackEvent method when the link
// This code creates is clicked.
Writer. Write ("<a href = \" javascript: "+ Page. GetPostBackEventReference (this," dec ") +" \ "> Decrease Number </a> ");
}
}

 

 

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.