.aspx page:
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox TextMode="password" ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
code behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For Each s As String In Request.Form.AllKeys
Response.Write(s & ": " & Request.Form(s) & "<br />")
Next
End Sub
Separate HTML page:
<form action="http://localhost/MyTestApp/Default.aspx" method="post">
<input name="TextBox1" type="text" value="" id="TextBox1" />
<input name="TextBox2" type="password" id="TextBox2" />
<input type="submit" name="Button1" value="Button" id="Button1" />
</form>
Form HTML:
<html>
<body>
<form id='postForm' action='WebForm.aspx' method='POST'>
<input type='text' name='postData' value='base-64-encoded-value' />
<input type='hidden' name='__VIEWSTATE' value='' /> <!-- still need __VIEWSTATE, even empty one -->
</form>
</body>
</html>
WebForm.aspx:
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="WebForm.aspx.cs" Inherits="WebForm"
EnableEventValidation="False" EnableViewState="false" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="postForm" runat="server">
<asp:TextBox ID="postData" runat="server"></asp:TextBox>
<div>
</div>
</form>
</body>
</html>
WebForm.cs:
public partial class WebForm: System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
string value = Encoding.Unicode.GetString(Convert.FromBase64String(this.postData.Text));
}
}
and you will get the values in aspx page after submit button click like this- protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) { CompleteRegistration(); } }
public void CompleteRegistration() {
NameValueCollection nv = Request.Form;
if (nv.Count != 0) {
string strname = nv["txtbox1"];
string strPwd = nv["txtbox2"];
}
}
05/10/2022
HTML forms use either GET or POST to send data to the server. The method attribute of the form element gives the HTTP method:
<form action="api/values" method="post">
Typically, you will send a complex type, composed of values taken from several form controls. Consider the following model that represents a status update:
namespace FormEncode.Models {
using System;
using System.ComponentModel.DataAnnotations;
public class Update {
[Required]
[MaxLength(140)]
public string Status {
get;
set;
}
public DateTime Date {
get;
set;
}
}
}
Here is a Web API controller that accepts an Update
object via POST.
namespace FormEncode.Controllers
{
using FormEncode.Models;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
public class UpdatesController : ApiController
{
static readonly Dictionary<Guid, Update> updates = new Dictionary<Guid, Update>();
[HttpPost]
[ActionName("Complex")]
public HttpResponseMessage PostComplex(Update update)
{
if (ModelState.IsValid && update != null)
{
// Convert any HTML markup in the status text.
update.Status = HttpUtility.HtmlEncode(update.Status);
// Assign a new ID.
var id = Guid.NewGuid();
updates[id] = update;
// Create a 201 response.
var response = new HttpResponseMessage(HttpStatusCode.Created)
{
Content = new StringContent(update.Status)
};
response.Headers.Location =
new Uri(Url.Link("DefaultApi", new { action = "status", id = id }));
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
[HttpGet]
public Update Status(Guid id)
{
Update update;
if (updates.TryGetValue(id, out update))
{
return update;
}
else
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
}
}
}
When the user clicks Submit, the browser sends an HTTP request similar to the following:
POST http: //localhost:38899/api/updates/complex HTTP/1.1
Accept: text / html, application / xhtml + xml, *
/*
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Content-Type: application/x-www-form-urlencoded
Content-Length: 47
status=Shopping+at+the+mall.&date=6%2F15%2F2012
The following code shows how to post form data using jQuery.
<script type="text/javascript">
$("#form1").submit(function () {
var jqxhr = $.post('api/updates/complex', $('#form1').serialize())
.success(function () {
var loc = jqxhr.getResponseHeader('Location');
var a = $('<a/>', { href: loc, text: loc });
$('#message').html(a);
})
.error(function () {
$('#message').html("Error posting the update.");
});
return false;
});
</script>
Use the cross-page posting functionality added to ASP.NET 2.0 to submit a form to a different page.,In the .aspx file of the first page, set to the URL of the desired page the PostBackUrl property of the button that will initiate the submission of the form to another page. No special code is required in the code-behind of the first page to support cross-page posting.,You cannot set the action attribute of the form element to cause the form to be submitted to another page. ASP.NET always changes the action to the URL of the page being displayed.,In the .aspx file of the second page, add the PreviousPageType directive to the top of the file with the VirtualPath attribute set to the URL of the first page:
<%@ Page Language="VB" MasterPageFile="~/ASPNetCookbookVB.master"
AutoEventWireup="false"
CodeFile="CH04SubmitToAnother_FirstPageVB1.aspx.vb"
Inherits="ASPNetCookbook.VBExamples.CH04SubmitToAnother_FirstPageVB1"
Title="Form Submission To Another Page - Approach 1" %>
<asp:Content ID="pageBody" Runat="server" ContentPlaceHolderID="PageBody">
<div align="center" class="pageHeading">
Form Submission To Another Page - Approach 1 (VB)
</div>
<table width="50%" align="center" border="0">
<tr>
<td class="labelText">First Name: </td>
<td>
<asp:TextBox ID="txtFirstName" Runat="server" Columns="30" CssClass="LabelText" />
</td>
</tr>
<tr>
<td class="labelText">Last Name: </td>
<td>
<asp:TextBox ID="txtLastName" Runat="server" Columns="30" CssClass="LabelText" />
</td>
</tr>
<tr>
<td class="labelText">Age: </td>
<td>
<asp:TextBox ID="txtAge" Runat="server" Columns="30" CssClass="LabelText" />
</td>
</tr>
<tr>
<td align="center" colspan="2">
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" PostBackUrl="~/CH04SubmitToAnother_SecondPageVB1.aspx" />
</td>
</tr>
</table>
</asp:Content>
Option Explicit On
Option Strict On
Namespace ASPNetCookbook.VBExamples
''' <summary>
''' This class provides the code behind for
''' CH04SubmitToAnother_FirstPageVB1.aspx
''' </summary>
Partial Class CH04SubmitToAnother_FirstPageVB1
Inherits System.Web.UI.Page
'''****************************************************************
''' <summary>
''' This routine provides the event handler for the page load event. It
''' is responsible for initializing the controls on the page.
''' </summary>
'''
'''
<param name="sender">Set to the sender of the event</param>
'''
<param name="e">Set to the event arguments</param>
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
End Sub 'Page_Load
End Class 'CH04SubmitToAnother_FirstPageVB1
End Namespace
using System;
namespace ASPNetCookbook.CSExamples
{
/// <summary>
/// This class provides the code behind for
/// CH04SubmitToAnother_FirstPageCS1.aspx
/// </summary>
public partial class CH04SubmitToAnother_FirstPageCS1 : System.Web.UI.Page
{
///*******************************************************************
/// <summary>
/// This routine provides the event handler for the page load event.
/// It is responsible for initializing the controls on the page.
/// </summary>
///
/// <param name="sender">Set to the sender of the event</param>
/// <param name="e">Set to the event arguments</param>
protected void Page_Load(object sender, EventArgs e)
{
} // Page_Load
} // CH04SubmitToAnother_FirstPageCS1
}
<%@ Page Language="VB" MasterPageFile="~/ASPNetCookbookVB.master"
AutoEventWireup="false"
CodeFile="CH04SubmitToAnother_SecondPageVB1.aspx.vb"
Inherits="ASPNetCookbook.VBExamples.CH04SubmitToAnother_SecondPageVB1"
Title="Form Submission To Another Page - Second Page - Approach 1" %>
<asp:Content ID="pageBody" Runat="server" ContentPlaceHolderID="PageBody">
<div align="center" class="pageHeading">
Form Submission To Another Page - Approach 1 (VB)
</div>
<table width="50%" align="center" border="0">
<tr>
<td colspan="2" align="center" class="pageHeading">
Data Submitted From Previous Page
</td>
</tr>
<tr>
<td class="labelText">First Name: </td>
<td class="labelText">
<asp:Label ID="lblFirstName" Runat="server" />
</td>
</tr>
<tr>
<td class="labelText">Last Name: </td>
<td class="labelText">
<asp:Label id="lblLastName" Runat="server" />
</td>
</tr>
<tr>
<td class="labelText">Age: </td>
<td class="labelText">
<asp:Label ID="lblAge" Runat="server" />
</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td class="labelText">Page Access Method: </td>
<td class="labelText">
<asp:Label ID="lblPageAccessMethod" Runat="server" />
</td>
</tr>
</table>
</asp:Content>
Option Explicit On
Option Strict On
Imports System.Web.UI.WebControls
Namespace ASPNetCookbook.VBExamples
''' <summary>
''' This class provides the code behind for
''' CH04SubmitToAnother_SecondPageVB1.aspx
''' </summary> Partial Class CH04SubmitToAnother_SecondPageVB1
Inherits System.Web.UI.Page
'''******************************************************************
''' <summary>
''' This routine provides the event handler for the page load event. It
''' is responsible for initializing the controls on the page.
''' </summary>
'''
'''
<param name="sender">Set to the sender of the event</param>
'''
<param name="e">Set to the event arguments</param>
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Dim tBox As TextBox
Dim pageContent As ContentPlaceHolder
'check to see how the page is being accessed
If (IsNothing(Page.PreviousPage)) Then
'page is not being accessed by cross-page post so check to if
'it is being accessed via self post-back
If (Page.IsPostBack) Then
'page is being accessed by a post-back from itself
lblPageAccessMethod.Text = "Page was accessed via post-back"
Else
'page is being accessed directly
lblPageAccessMethod.Text = "Page was accessed directly"
End If
Else
'page is being accessed by a cross-page post-back
lblPageAccessMethod.Text = "Page was accessed via cross-page post-back"
'get the page content control
'NOTE: This is required since a master page is being used and the
' controls that contain the data needed here are within it
pageContent = CType(Page.PreviousPage.Form.FindControl("PageBody"), _
ContentPlaceHolder)
'get the first name data from the first page and set the label
'on this page
tBox = CType(pageContent.FindControl("txtFirstName"), _
TextBox)
lblFirstName.Text = tBox.Text
'get the last name data from the first page and set the label
'on this page
tBox = CType(pageContent.FindControl("txtLastName"), _
TextBox)
lblLastName.Text = tBox.Text
'get the age data from the first page and set the label
'on this page
tBox = CType(pageContent.FindControl("txtAge"), _
TextBox)
lblAge.Text = tBox.Text
End If
End Sub 'Page_Load
End Class 'CH04SubmitToAnother_SecondPageVB1
End Namespace
help? What is 'CodeProject'? General FAQ Ask a Question Bugs and Suggestions Article Help Forum About Us , communitylounge Who's Who Most Valuable Professionals The Lounge The CodeProject Blog Where I Am: Member Photos The Insider News The Weird & The Wonderful
<form action="home.aspx" method="post" id="form1">
<input id="Text1" name="text1" type="text" />
<br />
<input id="Text2" type="text" name="text2" />
<br />
<input id="Button1" type="button" value="send" name="send" />
</form>
<input id="Button1" type="button" value="send" name="send" />
<input id="Button1" type="submit" value="send" name="send" />
Request.Form["your_text_box_name"]
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For Each s As String In Request.Form.AllKeys
Response.Write(s & ": " & Request.Form(s) & "<br />")
Next
End Sub