Posting to another .aspx using ASP.NET


As we know, ASP.NET wont allow to post back to another aspx page. It will post back to itself. If you specify action attribute it will ignore it. If you omit the method attribute, it will be set to method="post" by default. Also, if you do not specify the name and id attributes, they are automatically assigned by ASP.NET. An aspx can contain only one <form> tag.

There is one workaround for posting to another .aspx page. Omit the runat="server" attribute. What Runat attribute does is it will override the action attribute to the file name submitted. If you omit the runat attribute, action will take to another aspx page with the form values posted. But one disadvantage is there, we cant use server controls inside the first page. Those values cant be posted to another .aspx page. But one good thing is we can use HTML controls. Runat=server in HTML controls will allow us to access those values in the posted page.

Let us see one example, First.aspx

<html>
<
head><title>First Page</title></head>
<
body>
<
form name="frmFirstPage" action="Second.aspx" method="post">
<
input type="text" name="FirstName" id="FirstName" runat="server"/>
<
input type="text" name="LastName" id="LastName" runat="server"/>
<
input name="Submit" type="Submit" runat="server" ID="Submit1"/>
</
form>
</
body>
</
html>

Second.aspx

<html>
<
head><title>Second Page</title></head>
<
body>
<
form id="frmFirstPage" method="post" runat="server">
FirstName:<%=Response.Write(Request["FirstName"])%>
LastName:<%=Response.Write(Request["LastName"])%>
</form>
</
body>
</
html>

If u enter Naveena as FirstName and Maadhaven as second name in the First.aspx, you can see the output in second.aspx as

FirstName : Naveena
LastName : Maadhaven

This is the very simple example but this shows whatever we did using ASP is still available in ASP.NET. But you should go for this approach when you don't need any web server or rich controls in your application. Hope this article will be useful to you.

Up Next
    Ebook Download
    View all
    Learn
    View all