Cross-Page Posting of Form Contents For Aspnet Developers
Hi Freinds ,In this Article we will discuss very interesting topic about standard postback and cross postbackthe,
We all know that ASP.NET depends on the postback architecture,
where pages containing a <form> section post back to themselves. However,
there are cases when you may want to post the contents of a form to another page.
For example, you may need to generate sections of the page using ASP.NET server-side code but post the form to another page,
or have multiple submit buttons that post to different pages.
To support this, most of the controls that can cause a postback, such as the Button and LinkButton, expose the PostBackUrl property.
You can set this to the URL of a page that will handle the posted form contents.
Inside the target page, you can access the original page and read values from itin much the same way as you saw when looking at the Server.
Usually, the target page will be within the same ASP.NET application as the originating page. You can post to a page in another application,
but in this case you will only be able to extract the contents of the Request collections (Form, QueryString, Cookies, and Server Variables).
You will not be able to access the viewstate or properties of the originating page.
An Example of Cross-Page Posting
As a simple example of cross-page posting,it shows the controls on a page that cause both a standard postback to the same page and
a cross-page postback to another page. The source page contains a drop-down list named EmpNames.
A Label control is included to show the properties of the page when a standard postback occurs.
Listing 10.17. The Controls in the Page That Cause a Cross-Page Postback
This is the first page
select a EmpName:
<asp:DropDownList id="Empname" runat="server"> <asp:ListItem text="emp1" value="0" /> <asp:ListItem text="emp2" value="1" /> <asp:ListItem text="emp3" value="2" /> </asp:DropDownList> <asp:Button id="Button1" runat="server" Text="Standard" OnClick="Button1_Click" />Cause a postback to the same page
<asp:Button id="Button2" runat="server" Text="Cross" PostBackUrl="cross-page.aspx" />Cause a postback to a different page
<asp:Label ID="Label1" runat="server" />This source page also contains a server-side script section,
This code exposes a public property for the page.In this case, the property named SelectedEmployee Name returns a reference to the dropdown list control on this page.
Listing 10.18. The Code in the Page That Causes a Cross-Page Postback
// public property exposed to next page
public DropDownList SelectedEmployee { get { return Empname; } } void Button1_Click(Object sender, EventArgs e) { Label1.Text = "Page posted back to itself.<br />" + "Page URL = " + Request.Url.LocalPath + "<br />" + "Page.IsPostBack = " + Page.IsPostBack.ToString() + "<br />" + "Page.IsCrossPagePostBack = " + Page.IsCrossPagePostBack.To String(); }The remaining code is an event handler for the first button on this page, which causes a standard postback.
This event handler displays a message containing the URL of the current page and the value of the IsPostBack and IsCrossPagePostBack properties of the Page object.
The second button in the source page, has the attribute PostBackUrl="cross-page.aspx",
so when it is clicked, it will cause a postback to the target page catch-cross-page.aspx rather than to the source page.
This target page contains the PreviousPageType directive, which points to the original source page:
<%@ PreviousPageType VirtualPath="cross-page.aspx" %>An alternative approach is to declare a class type name in the source page:
<%@ Page ClassName="MySourcePage"...%>Then you can specify this type name in the PreviousPageType directive:
<%@ PreviousPageType TypeName="MySourcePage"%>There is also a Label control on the target page populated in the Page_Load event handler of this page, as shown in Listing 10.19.
The Code in the Target Page for a Cross-Page Postback
void Page_Load() { if (PreviousPage != null) { PrevMessage.Text = "On the previous page you selected <b>" + PreviousPage.SelectedCountry.SelectedItem.Text + "</b>.<br />" + "Page URL = " + Request.Url.LocalPath + "<br />" + "Page.IsPostBack = " + Page.IsPostBack.ToString() + "<br />" + "Page.IsCrossPagePostBack = " + Page.IsCrossPagePostBack.ToString() + "<br />" + "PreviousPage.IsPostBack = " + PreviousPage.IsPostBack.ToString() + "<br />" + "PreviousPage.IsCrossPagePostBack = " + PreviousPage.IsCrossPagePostBack.ToString(); } else { PrevMessage.Text = "Not a cross-page postback."; } }This code first checks that the page is loading in response to a cross-page post,
where the PreviousPage property will be a reference to the page that causes the cross-page postback.
If the page is loaded directly, the PreviousPage property will be null, and so the code will display an error message in this case.
Providing that there is a reference available to the previous page,
the code then continues by displaying the Text property of the SelectedItem of the Country drop-down list on the previous page.
It can obtain this control reference from the public property exposed by the source page through the PreviousPage property.
Then the code displays the URL of this (target) page, followed by the IsPostBack and IsCrossPagePostBack properties.
Finally, it accesses the previous (source) page again to display the value of its IsPostBack and IsCrossPagePostBack properties.
That's it friends...
Labels: .Net Tutorials, Aspnet, C#, CSharp, Tanisha
3 Comments:
Welcome@Azeem
This is a nice article..
Its very easy to understand ..
And this article is using to learn something about it..
c#, dot.net, php tutorial
Thanks a lot..!
I do not even understand how I finished up right here, but I assumed this submit was once good. I don't recognize who you're however definitely you are going to a well-known blogger when you aren't already. Cheers!
my site: vps hosting
Post a Comment
Subscribe to Post Comments [Atom]
<< Home