asp.net 2.0 cross-page callback

Source: Internet
Author: User

In asp.net2.0, cross-page submissions allow a callback callback for a page to be submitted to a different Web page. In this article, we'll explore some different ways to cross-page submissions. In addition, translation of the special difficulties, please friends to criticize.

asp.net1.1 only provides a way to submit to this page. In many cases, there is a need for a cross-page submission in our solution, and the traditional approach is to move to another page by Response,redirect or Server.Transfer and then simulate the effect of a cross-page callback.

ASP.NET2.0 provides this ability to implement a cross-page submission from one form page to another.

How to implement a cross-page submission

To implement a cross-page submission, on the source form page, you need to set the control's PostBackUrl this property to implement the IButtonControl (such as Button,imagebutton,linkbutton) interface to navigate to the target form page. When the user clicks the button control, the form page is submitted to the target form page across pages. You do not need to make any settings or write any code on the source form page.

Retrieving information from a source form page in a target page by using the FindControl method

The target form page gets the "cross" page The requested information is through a previouspage method. This property represents the source form page and establishes a reference for the source form page and its controls.

The controls on the source form page are available on the target page through the PreviousPage FindControl method.

protected void Page_Load(object sender, EventArgs e)
   {
   ...
   TextBox txtStartDate = (TextBox) PreviousPage.FindControl ("txtStartDate ");
   ...
   }

The target form page is not aware of any information about the source form page. PreviousPage the type of this property is page, so retrieving the control can use the FindControl method, but the developer needs to have a general understanding of the structure of the source form page. There are certain limitations to using the FindControl method, such as the FindControl method can only rely on the id attribute of the control provided by the developer to navigate to a control. This method may fail if the control ID of the source form page changes. The FindControl method can only handle controls that are in the current container, and if the developer needs to retrieve controls in other controls (the container), first you need to obtain a reference to the parent control.

Retrieving information from the source form page in the target page through the @previouspagetype directive

Another approach is to use the @previouspagetype directive when the source form page has been identified. This instruction can access the source form page in a strongly typed manner on the target form page. This instruction specifies whether the source form page uses the VirtualPath property or the TypeName property. The PreviousPage property returns a strongly typed result to reference the source form page. It allows access to the public properties of the source target page.

Sourcepage.aspx:

<form runat="server" >
...
<asp:textbox runat="server" id="txtFirstName"/>
<asp:textbox runat="server" id="txtLastName"/>
<asp:button runat="server" id="btnViewReport" Text="View Report" PostbackURL="~/targetpage.aspx" />
...
public string FirstName
{
   get { return txtFirstName.Text; }
}
...
TargetPage.aspx
<%@ PreviousPageType VirtualPath="sourcepage.aspx" %>
string strFirstName;
strFirstName = PreviousPage.FirstName //Strongly Typed PreviousPage allows direct access to the public properties of the source page.

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.