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

来源:互联网 发布:淘宝卖家快递合作 编辑:程序博客网 时间:2024/05/16 13:47

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 namePublic ReadOnly Property Name() As StringGetReturn USerName.TextEnd GetEnd Property'Readonly Property for phonePublic ReadOnly Property Phone() As StringGetReturn UserPhone.TextEnd GetEnd Property'Event to transfer page control to Result.aspxSub 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 SenderClassSub Page_load(obj as Object, e as EventArgs)Dim content As StringIf Not IsPostBack Thenresult = CType(Context.Handler, SenderClass)content = "Name: " + result.Name + "<br>" _+ "Phone: " + result.PhoneLabel1.text = contentEnd 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.aspxSub Page_Transfer (sender as Object, e as EventArgs)'Storing in ContextContext.Items("UserName") = UserName.textContext.Items("UserPhone") = UserPhone.textServer.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".

原创粉丝点击