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.
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:
- UserForm.aspx
- 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!
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".
本文探讨了ASP.NET中从一个页面向另一个页面提交Web表单数据的不同方法。由于无法使用ASP.NET Web控件在页面间提交数据,文章介绍了使用HttpContext对象等方式,还给出了两种利用Context对象传输数据的方法,此外还可使用Session对象和查询字符串。
896

被折叠的 条评论
为什么被折叠?



