In this article I will explain with an example, how to Redirect to another page using Post method from code behind in ASP.Net using C# and VB.Net.
 
 

HTML Markup

The HTML Markup consists of:
TextBox - For capturing Name.
Button - For post value of TextBox to new page from code behind.
The Button has been assigned with an OnClick event handler.
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td>First Name:</td>
        <td><asp:TextBox ID="txtFirstName" runat="server" /></td>
    </tr>
    <tr>
        <td>Last Name:</td>
        <td><asp:TextBox ID="txtLastName" runat="server" /></td>
    </tr>
    <tr>
        <td></td>
        <td><asp:Button ID="btnPost" runat="server" Text="Post" OnClick="PostData" /></td>
    </tr>
</table>
 
 

Namespaces

You will need to import the following namespaces.
C#
using System.Text;
using System.Collections.Specialized;
 
VB.Net
Imports System.Text
Imports System.Collections.Specialized
 
 

Redirecting to another page using Post method from code behind

When the Post Button is clicked, the values of TextBoxes are added to a NameValueCollection object.
Note: NameValueCollection is a collection of object having two parts i.e. Name and Value.
 
Then a HTML page consisting of a FORM tag is generated using string concatenation. The URL of the destination page is set in the action attribute of the FORM tag.
A loop is executed over the NameValueCollection and HTML INPUT TextBoxes are appended to the string. The Name part of the NameValueCollection is set in the name attribute of the TextBox, while the Value part is set in the value attribute of the TextBox.
Inside the BODY tag of the HTML page, a JavaScript event is defined, which will automatically submit the Form as soon as the HTML string is rendered on the page.
Finally, the HTML string is written to the Response using Reponse.Write method.
protected void PostData(object sender, EventArgs e)
{
    NameValueCollection collections = new NameValueCollection();
    collections.Add("FirstName", txtFirstName.Text.Trim());
    collections.Add("LastName", txtLastName.Text.Trim());
    string remoteUrl = "/Page2_CS.aspx";
 
    string  html = "<html><head>";
    html += "</head><body onload='document.forms[0].submit()'>";
    html += string.Format("<form name='PostForm' method='POST' action='{0}'>", remoteUrl);
    foreach (string key in collections.Keys)
    {
         html += string.Format("<input name='{0}' type='text' value='{1}'>", key, collections[key]);
    }
    html += "</form></body></html>";
    Response.Clear();
    Response.ContentEncoding = Encoding.GetEncoding("ISO-8859-1");
    Response.HeaderEncoding = Encoding.GetEncoding("ISO-8859-1");
    Response.Charset = "ISO-8859-1";
    Response.Write(html);
    Response.End();
}
 
VB.Net
Protected Sub PostData(sender As Object, e As EventArgs)
    Dim collections As New NameValueCollection()
    collections.Add("FirstName", txtFirstName.Text.Trim())
    collections.Add("LastName", txtLastName.Text.Trim())
    Dim remoteUrl As String "/Page2_VB.aspx"
 
    Dim html As String "<html><head>"
    html += "</head><body onload='document.forms[0].submit()'>"
    html += String.Format("<form name='PostForm' method='POST' action='{0}'>", remoteUrl)
    For Each key As String In collections.Keys
         html += String.Format("<input name='{0}' type='text' value='{1}'>", key, collections(key))
    Next
    html += "</form></body></html>"
    Response.Clear()
    Response.ContentEncoding = Encoding.GetEncoding("ISO-8859-1")
    Response.HeaderEncoding = Encoding.GetEncoding("ISO-8859-1")
    Response.Charset = "ISO-8859-1"
    Response.Write(html)
    Response.End()
End Sub
 
 

Fetching the Posted Data

HTML Markup

The HTML Markup consists of:
Label – For display posted value on another page.
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td>FirstName:</td>
        <td><asp:Label ID="lblFirstName" runat="server" /></td>
    </tr>
    <tr>
        <td>LastName:</td>
        <td><asp:Label ID="lblLastName" runat="server" /></td>
    </tr>
</table>
 
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (Request.Form.Count > 0)
    {
        lblFirstName.Text = Request.Form["FirstName"];
        lblLastName.Text = Request.Form["LastName"];
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Request.Form.Count > 0 Then
        lblFirstName.Text = Request.Form("FirstName")
        lblLastName.Text = Request.Form("LastName")
    End If
End Sub
 
 

Screenshot

Redirect to another page using Post method from code behind in ASP.Net using C# and VB.Net
 
 

Downloads