Cross page posting Submit Form Server.Transfer methods ASP.NET

Cross Page Posting Submit Form Server.Transfer Methods In ASP.NET.To transer data using cross page posting we need to use FindControl Method we can write code like this

First of all we need to set the PostBackUrl property of the Button we are gonna use for postback, to the page where we need to redirect, in our case it's Default2.aspx

to retrieve the value of a textbox from previous page in Default2.aspx page
we need to use FindControl method to find the textbox control of previous page

TextBox txtName = (TextBox)PreviousPage.FindControl("txtUserName"));

lblName.text = txtName.Text.ToString();


Submit Form Method to post data and retrieve on another page in ASP.NET

To start with simplest method we can write code like this

<form id="form1" method="post" 
action="Default2.aspx">
Data <input type="text" 
name="Data" size="20"><br>

<input id="Submit1" type="submit" 
value="submit" />
</form>

And to retrieve it on the posted page

lblValue.Text = Request.Form["Data"];


asp.net does not support post method with runat="Server" attribute,
for this we will have to use javascript

<form id="Form1" method="post"
runat="server">
<asp:TextBox ID="txtData" runat="server">
</asp:TextBox>
</form>

<form name="FormSubmit" action="Default2.aspx"
method="post">
<input id="Submit1" type="submit"
value="submit"
onclick="CopyTextToHiddenField()" />
<input name="Hidden1" type="hidden" />
</form>

Ass this JavaScript in the Head section of page to be posted

<script language="javascript">
function CopyTextToHiddenField()
{
var textbox1Value = document.getElementById
("<%=TextBox1.ClientID%>").value;

document.forms[1].document.getElementById
("Hidden1").value = textbox1Value;
}
</script>

To retrieve the value of hidden field
in Default2.aspx page

lblData.Text = Request.Form["Hidden1"];



Server.Transfer Method in ASP.NET

Server.Transfer method sends (transfers) all the state information (all application/session variables and all items in the request collections) created in one ASPX page to a second ASPX page.

you can't use Server.Transfer to send the user to an external site
The Server.Transfer method also has a second parameter—"preserveForm". If you set this to True, using a statement such as Server.Transfer("WebForm2.aspx", True), the existing query string and any form variables will still be available to the page you are transferring to.

For example, if your Default.aspx has a TextBox control called TextBox1 and you transferred to Default2.aspx with the preserveForm parameter set to True, you'd be able to retrieve the value of the original page TextBox control by referencing Request.Form("TextBox1").

protected void Button1_Click1
(object sender, EventArgs e)
{
Server.Transfer("Default2.aspx", true);
}

Now in Default2.aspx page_load event
use the below code to get the value of TextBox1

Response.Write(Request.Form["TextBox1"]);



Related Posts:
1. Data Transfer to other aspx pages using Query String Session cookies and cross page posting

2. Data Transfer Using Cookies Session Variables Cross page posting and QueryStrings in ASP.NET

3. LinkButton in GridView and QueryString Parameters to pass/transfer variable and data-ASP.NET Articles


If you like this post than join us or share

25 comments:

Jen said...

The blog is helpfull...
visit also asp.net [c#]


Rajendra Bhole said...

The concept of "ASP.NET Bypass forms authentication or Skip Authorization for selected pages" is really very helpful for me,when i was facing such kind of problem.With the help of this code my application has been made very simple and with also reduced the pages.


Anonymous said...

i have a gridview with a checkbox as a templatefield and a asp button out side the gridview. i want to store the data in another table of gridview which i have selected by checkbox. plz kindly help me out...thank you


Anonymous said...

d


Anonymous said...

hey how to dowmload this....??


Anonymous said...

Great post thanks


Anonymous said...

nice blog sir...go ahead and keep on teaching us a lastest methodologies in .NET thru ur postings


Thanq sir


Anonymous said...

Hello.
The interesting name of a site - csharpdotnetfreak.blogspot.com, interesting this here is very good.
I spent 4 hours searching in the network, until find your forum!


Anonymous said...

This comment has been removed by a blog administrator.


Anonymous said...

Unknown message


Anonymous said...

Hey im new here.

Nice to meet everyone!
Hope you all have a good day


Anonymous said...

Hey im new here.

Im sam, how is everyone?

I look forwards to being a active memeber


Anonymous said...

I agree, it's interesting, probably I come in handy


Anonymous said...

Stop hack the program!!!


Anonymous said...

“How awful.”


Anonymous said...

Welcome test


Anonymous said...

What's up all members, I am a brand-new member on this bulletin board therefore I think I should introduce myself. Erm... I am 24 years of age, a girl, and I enjoy studying german in my university. I certainly can't wait chatting with you older members! Goodbye for now


Anonymous said...

My mother expected I would like this website. It was fully accurate. This post actually made my day. You can't conceive of simply how greatly time I had spent for this content! Thanks!

Before I go, allow me thank you for your tolerance with my English as (I'm certain you have become aware this by now,), English is not my principal tongue accordingly I am utilizing Google Translate to build out how to compose what I really wish to state.


Anonymous said...

Hack again?!


Anonymous said...

Your web page looks extraordinary - full with superior subject matter.. with so on. I expect you possibly will develop still even healthier article through including some videos and photographs. I anticipate this is an ok means to state it. While I have put it in my favorites . Thanks.

In conclusion , allow me thank you for your patience with my English as (I am convinced you have become aware this by now,), English is not my primary language as a result I am using Google Translate to build out what to write down what I truly wish to say.


Anonymous said...

This comment has been removed by a blog administrator.


Anonymous said...

Hi Fellowships everything fine? at this moment this is it my primary pice of writing on this community, awesome community by the way.
Lets head to my presentation, I´m Jack, I´m from Madrid, now I'm taking a semester on a different place at the time on Computer Engenieering to obtain my diplome.
I am very found of books as well as sleeping lol, and I also enjoy a lot Metallica, my greatest hobbie now is playing poker, as most of you...I think....and also board games.
The main reason I signed up on this board to search for ideas to my websites at [url=http://www.dinerogratispokeronline.com/public_html/tilatequila/]Tila Tequila[/url] ...You can not blame me because you guys have a lot interesting topics here, but off course I will participate activatly in all of the discussions in the forum!!!!!
I will also apoligize by my writting it is the only way I found to communicate with you....
And for today it is all I have to say, because work by shifts and now I will spleep a few hours..............Just hopping you apreciated my first post.
By Bye to you all, love you all


Anonymous said...

Unknown message


Anonymous said...

i think not all of you agree with that .. but i have to say

an arab ... learn the languge :d
thanks


Anonymous said...

Nice post


Find More Articles