Passing Values from one Page to Another using Post Method in ASP.NET

This blog discusses the method of passing values from one page to another using Post Method in ASP.NET.

protected void Page_Load(object sender, EventArgs e)

{

     string postbackUrl = "www.example.com"; // url where the user needs to be redirected.

     string userName = Convert.ToString(Session["Loginid"]); // Assigned the value of user name in a string.

     userName = EncryptSession.Encrypt(userName);// Encrypted the user name.

     string url = "www.loginurl.com"; // url from where the user logins.

      Response.Clear();

      StringBuilder sb = new StringBuilder();

      sb.Append("<html>");

      sb.AppendFormat(@"<body onload='document.forms[""form""].submit()'>");

      sb.AppendFormat("<form name='form' action='{0}' method='post'>", postbackUrl);

      sb.AppendFormat("<input type='hidden' name='userName' value='{0}'>&", userName);

      sb.AppendFormat("<input type='hidden' name='url' value='{0}'>", url);

      // Other params go here

      sb.Append("</form>");

      sb.Append("</body>");

      sb.Append("</html>");

      Response.Write(sb.ToString());

      Response.End();

}

Ebook Download
View all
Learn
View all