[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.
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:
- 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.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!
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".
- [ZT]Submitting Web Form data from one ASP.NET page to another
- Transferring Data from One Table to Another
- How to move Oracle data from one machine to another
- sql server 2008 copy data from one table to another
- 5.3 Passing Data From One Screen to Another
- Moving entities from one AutoCAD layer to another using .NET
- Pass data from a ASP.NET page to ASCX user controls loaded dynamically
- Copy Form from one Client ot Another
- How to pass ViewState value from web form page to web user control page?
- How to export/import data with LOB type from one user/schema to another
- Migrating Queries from One User To Another
- copy from one array to another
- Transfer Statistics from one Database to Another
- How can we use transfer function to skip search page while going from one component to another?
- use meta header to redirect a page to another one
- SQL SERVER – Insert Data From One Table to Another Table – INSERT INTO SELECT – SELECT INTO TABLE
- BAPI to Copy Materials from one Plant to Another
- BAPI to Copy Materials from one Plant to Another
- 真正去考虑问题
- D语言概览(一)
- 属性文件在JAVA中的应用
- 我最深爱的女人
- 回忆在KINGDEE工作的日子(四)
- [ZT]Submitting Web Form data from one ASP.NET page to another
- 回忆在金蝶工作的日子(一)
- 明天就回家过年了……
- 放假前夜
- 回忆在KINGDEE工作的日子(二)
- 回忆在金蝶工作的日子(三)
- 我的第一篇CSDN-Blog文章
- 被扣1000块工资了
- 利用Shell API查找指定后缀文件名在本机的注册程序