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

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.

Method 2
This method also uses Context object to retrieve the data. It's simple and straight forward, you don't need to create property procedures at all.

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

<script runat="server">
	'Event to transfer page control to Result.aspx
	Sub Page_Transfer (sender as Object, e as EventArgs)
		'Storing in Context
		Context.Items("UserName") = UserName.text
		Context.Items("UserPhone") = UserPhone.text

		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" %>

<html>
<head>
</head>
<body>
	Name: <%= Context.Items("UserName") %><br>
	Phone: <%= Context.Items("UserPhone") %>
</body>
</html>

That's it, pretty cool ah!

Note: I have stored the textbox value in context before transferring control to Result.aspx

Apart from the above two methods still you can use Session object and old query string to transfer data. I personally feel event driven model of ASP.NET Web Forms is much better than the Classic ASP one, of course approach will differ from developer to developer.

Happy .Net "ing".

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值