Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article
Stardeveloper RSS Feed
Hosted by Securewebs.com
 
Home : .NET : ASP.NET : Submitting Web Form data from one ASP.NET page to another
 
RSS - Read full length articles at Stardeveloper using Stardeveloper RSS Feed RSS

Submitting Web Form data from one ASP.NET page to another

by Senthil Ramasamy.

This article discusses different options you as a developer have in ASP.NET to send data from one ASP.NET page to another. Since you cannot use ASP.NET Web Controls (System.Web.UI.WebControls) in such a scenario (which only allow posting back of data to the same page), this article discusses other ways like using HttpContext object.

This article is about submitting data from Web Forms (forms with runat="server" attribute). You easily submit your form data to another page by removing the runat="server" attribute, then you cannot use more advanced ASP.NET Server controls.

Note: Here the term 'Web Forms' refers to Form tag with runat="server" attribute.

In ASP.NET Web Forms you cannot submit your form data from one page to another using ASP.NET Web Controls (System.Web.UI.WebControls), it is always posted back to the same page. Of course ASP.NET Web Forms still support the classic ASP style of submitting the form data to another page. You can mimic the wizard style approach common in Classic ASP by using any one of the following methods:

Method 1
Using Property Procedure and Context to transfer web form data to another page Read the program given below:

<!-- UserForm.aspx -->
<%@ Page Language="VB" ClassName="SenderClass" %>

<script runat="server">
	' Readonly property for name
	Public ReadOnly Property Name() As String
		Get
			Return USerName.Text
		End Get
	End Property

	'Readonly Property for phone
	Public ReadOnly Property Phone() As String
		Get
			Return UserPhone.Text
		End Get
	End Property

	'Event to transfer page control to Result.aspx
	Sub Page_Transfer(sender As Object, e As EventArgs)
		Server.Transfer("Result.aspx")
	End sub
</script>

<html>
<head>
</head>
<body>
	<form runat="server">
		User Name: 
		<asp:TextBox ID="UserName" runat="server" />
		Phone: 
		<asp:TextBox ID="UserPhone" runat="server" /><br>
		<asp:Button Text="Submit" OnClick="Page_Transfer"
			runat="server" />
	</form>
</body>
</html>

<!-- Result.aspx -->
<%@ Page Language="VB" %>
<%@ Reference Page="UserForm.aspx" %>

<script runat="server">
    Dim result As SenderClass

	Sub Page_load(obj as Object, e as EventArgs)
		Dim content As String

		If Not IsPostBack Then
			result = CType(Context.Handler, SenderClass)
			content = "Name: " + result.Name + "<br>" _
				+ "Phone: " + result.Phone
			Label1.text = content
		End If
    End Sub

</script>
<html>
<head>
</head>
<body>
	<form runat="server">
		<asp:Label id="Label1" runat="server" />
	</form>
</body>
</html>

I have created two ASP.NET files:

  1. UserForm.aspx
  2. Result.aspx

UserForm.aspx displays two textboxes and a button where user can type his/her name and phone number. Button control will raise the event Page_Transfer, In page Transfer event I used Server object to transfer control to Result.aspx. In the <Script runat="server" /> tag I have created readonly property procedure for Name and Phone number. In the Page directive I have included the attribute ClassName="SenderClass", that's it.

In Result.aspx I referred the page(UserForm.aspx) using <%@ Reference Page="UserForm.aspx" %> directive, then inside the Page_Load() event I converted the Context.Handler to SenderClass. After the execution of this statement your result(Object variable) is connected to SenderClass Object so you can refer the property's available in the SenderClass like result.Name, which will execute Name() property and return the value of UserName in UserHome.aspx and result.Phone will return user phone number.

I have used Context object, The Context object is initialized at the start of each request and will last until the end of that request. So Context object provides the ease of getting the data from one page to another.


 ( 1 Remaining ) Next

Comments/Questions ( Threads: 8, Comments: 16 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. About Queries
  2. really too good
  3. Submitting Web Form data from one ASP.NET page to another with respective url
  4. Appying the techniques in a multiple CheckBoxList
  5. Calling a method (in a code behind on one aspx page) from another aspx page
  6. Question
  7. how to keep passing the data ( 2 Replies )
  8. Code Fix ( 6 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 2 replies by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.