C# .NET - cross-page postback - Asked By jeethu on 04-Oct-10 03:17 AM

how to pass values through values from one page to another using cross-page postback?
Reena Jain replied to jeethu on 04-Oct-10 03:22 AM
Hi jeethu

Hi you can pass the value for page to another page use following

Session
Querystring
cookies
HttpContext

Passing Values Using Cross Page Postback

Using cross page postback means posting the source page to a different aspx page. By default, all aspx pages will post to itself. You can understand this when you see the action attribute of form tag which will have the value as the same page name. This means, clicking a submit button on the page will post to the same page on the server.  Coming to our subject, you can use this technique to post the values in source page to a destination page using cross page postback.

 

There are multiple ways of implementing it. Some are below,

1.    You can set the action attribute of form tag as Destination page,

<form id="form1" runat="server" action="Destination.aspx">

 

2.    You can use jquery or javascript to chaneg action attibute of form tag to destination page.

<script src="../scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

   <script type="text/javascript">

     $(document).submit(function() {

     $("form").attr("action", "Destination.aspx");

     });

   </script>

here is some more link for help

http://www.codedigest.com/Articles/ASPNET/351_How_to_Pass_Values_from_One_Page_to_Another_in_ASPNet.aspx

http://msdn.microsoft.com/en-us/library/ms178139.aspx

Sagar P replied to jeethu on 04-Oct-10 03:25 AM
If you are using Cross Page posting then you can easily get all controls values to other page by using PreviousPage like; if you have page where 2 text boxes and one button which will use cross page posting to redirect to next page;

<form id="form1" runat="server">
  <div>
  <h2> This page will cross post to another page by setting PostbackUrl property of submit button.</h2>
    &nbsp;
    <asp:Label ID="lblName" runat="server" Text="LoginName" style="left: 76px; position: relative; top: 20px"></asp:Label><br />
    &nbsp;<asp:TextBox ID="txtPassword" runat="server" style="z-index: 100; left: 160px; position: relative; top: 34px"></asp:TextBox>
    <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
    <asp:Button ID="btnSubmit" runat="server" Text="Submit"      
    PostBackUrl="DestinationPage.aspx" style="left: 208px; position: relative; top: 54px" />
    <br />
   <asp:Label ID="lblPassword" runat="server" Text="Password" style="left: 90px; position: relative; top: -12px" Height="4px" Width="43px"></asp:Label></div>
</form>

So on destination page you can access the values of these text boxes like;

Label1.Text = "Name:" + CType(PreviousPage.FindControl("txtName"), TextBox).Text
Label2.Text = "Password:" + CType(PreviousPage.FindControl("txtPassword"), TextBox).Text

Check out this link to know more abt state management in case of Cross page posting;

http://www.informit.com/articles/article.aspx?p=680819
Jatin Prajapati replied to jeethu on 04-Oct-10 03:33 AM
Hi Jeethu.

I suppose that you have used the Button's post BackUrl for Cross-page postback or set the forms action to your destination page.

So on the destination page you can retrieve your values from previous page using following code snippet.

Suppose on your Page1.aspx you have a text box named txtName. When you do cross-page postback to Page2.aspx you can use string name = Request.Form["txtName"];  This will give you the value of txtName textbox on your Page1.aspx. Same way you can get other text box values.

Thanks.
DL M replied to jeethu on 04-Oct-10 03:39 AM
You can get information from below link will help.

http://www.asp.net/general/videos/how-do-i-pass-information-from-one-page-to-another-using-a-query-string
http://www.asp.net/general/videos/how-do-i-implement-a-cross-page-postback-in-aspnet

http://www.devproconnections.com/article/aspnet2/cross-page-postbacks.aspxhttp://www.asp.net/learn/videos/video-375.aspxhttp://www.asp.net/learn/videos/video-375.aspxhttp://www.asp.net/learn/videos/video-375.aspx
jeethu replied to Sagar P on 04-Oct-10 04:55 AM

thnks
 
i didn't get the function CType().
jeethu replied to Jatin Prajapati on 04-Oct-10 04:57 AM
Thanks 4 giving the simplest way to cross-page postback.
Sagar P replied to jeethu on 04-Oct-10 05:30 AM
CType is used just to convert the control into TextBox... its like out TypeCasting ....
So the statement like this CType(PreviousPage.FindControl("txtName"), TextBox) will convert control to TextBox so that we can use Text property to get the text of text box....
It a sytax for type casting.....