Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Let's say I have one html web-page (form.html) containing a simple form which is sent using POST method:
HTML
<body>
   <form name="Form1" method="POST" action="process.aspx">
      <input name="firstname" type="text" value="" />
      <input name="lastname" type="text" value="" />
      <input type="submit" value="Send" name="submit" />
   </form>
</body>


Now, in another page at the same directory (process.aspx) I want to process the data in some way. How do I fetch this data? What I tried to do is:
C#
string firstname = Request.Form["firstname"];
string lastname = Request.Form["lastname"];


It didn't work. What's the right way to do this?
Posted
Updated 26-May-19 22:09pm
Comments
F-ES Sitecore 19-Feb-16 4:46am    
Your code is fine, the problem must lie elsewhere with something about your solution you're not telling us.

Refer - Read Post Data submitted to ASP.Net Form[^]
Quote:
Read the Request.Form NameValueCollection and process your logic accordingly:

C#
NameValueCollection nvc = Request.Form;
string userName, password;
if (!string.IsNullOrEmpty(nvc["txtUserName"]))
{
  userName = nvc["txtUserName"];
}

if (!string.IsNullOrEmpty(nvc["txtPassword"]))
{
  password = nvc["txtPassword"];
}

//Process login
CheckLogin(userName, password);

... where "txtUserName" and "txtPassword" are the Names of the controls on the posting page.
 
Share this answer
 
Comments
Mukarram92 5-Jun-17 5:04am    
I am trying to receive data from an Android app, The app is posting data using post method, and I want to receive that data in asp.net web form web services, in the class file. The extension on chrome is .ASMX, following is the code. I wanna do it with Post method.

[WebMethod]
[ScriptMethod(UseHttpGet =true)]
public void GetComplaintInJson()
{

string CID = GetJson[0];
string strModelNo = GetJson[1];
string strSerailNo = GetJson[2];
string strRemarks = GetJson[3];
string strProInfo = GetJson[4];
string strDate =GetJson[5];
string strMdate = GetJson[6];

dynamic Cdata = JObject.Parse(CID);
string CustID = Cdata.CustomerID;

dynamic Mdata = JObject.Parse(strModelNo);
string ModNo = Mdata.ModelNo;


//========= Converting image form json to string then string to image and uploading in folder=====//
MemoryStream ms = new MemoryStream(img, 0, img.Length);
ms.Write(img, 0, img.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);

tbl_Complaints obj = new tbl_Complaints();
obj.OrderBy = "CompID desc";
DataTable dtbl = obj.Select();
int CompID = 0;
if (dtbl.Rows.Count > 0)
{
CompID = clsPrecaution.GetInt_Zero(dtbl.Rows[0]["CompID"]);
CompID = CompID + 1;
}
string strFilePath = "~/img/ComplaintImg/" + CompID.ToString() + ".Jpeg";
System.Drawing.Imaging.ImageFormat.Jpeg);
//=================Image===================================//



dynamic SerNoData = JObject.Parse(strSerailNo);
string SrNo = SerNoData.SerialNo;

dynamic RemData = JObject.Parse(strRemarks);
string Remarks = RemData.ComplaintRemark;

dynamic ProData = JObject.Parse(strProInfo);
string ProductInfo = ProData.ProductInfo;

dynamic DTData = JObject.Parse(strDate);
string Date = DTData.Date;

dynamic MDTDate = JObject.Parse(strMdate);
string MDate = MDTDate.MDate;

tbl_RegProductDetails objselect = new tbl_RegProductDetails();
objselect.WhereClause = "ModelNo='" + ModNo + "'";
DataTable dt = objselect.Select();


if (dt.Rows.Count > 0)
{
tbl_Complaints obc = new tbl_Complaints();
DataTable dtb = obc.Select();
String TicketNo = clsPrecaution.GetString_Null(dtb.Rows[0]["TicketNo"]);


int ONumber, ONO = getIndexofNumber(TicketNo);
string numberpart = TicketNo.Substring(ONO, TicketNo.Length - ONO);
ONumber = Convert.ToInt32(numberpart);
TicketNo = Convert.ToString("T" + (ONumber + 00001));

Date = ClsProject.GetMMDD_Current(Date);
MDate = ClsProject.GetMMDD_Current(MDate);

Hashtable hstbl = new Hashtable();
hstbl.Add("TicketNo", TicketNo);
hstbl.Add("CID", CID);
hstbl.Add("ModelNo", strModelNo);
hstbl.Add("SerialNo", strSerailNo);
hstbl.Add("ProdInfo", strProInfo);
hstbl.Add("Photo", strFilePath);
hstbl.Add("CompRemark", strRemarks);
hstbl.Add("CreateDate", strDate);
hstbl.Add("ModifyDate", strMdate);

tbl_Complaints Compobj = new tbl_Complaints();
Compobj.Data = hstbl;
Compobj.Add();

tbl_Complaints objshow = new tbl_Complaints();
objshow.WhereClause = "TicketNo='" + TicketNo + "'";
DataTable datbl = objshow.Select();
int ComplaintID = Convert.ToInt32(datbl.Rows[0]["CompID"]);

HttpContext.Current.Response.ContentType = "application/json";
HttpContext.Current.Response.Write("CompID=" + ComplaintID);
HttpContext.Current.Response.End();
}
else
{
HttpContext.Current.Response.Write("Invalid Model No");
}
What is the problem?
Mukarram92 7-Jul-17 8:34am    
The Android developer posting the data from the mobile application, to receive that record I have created above code and I am not able to receive that record, Android developer said that he is using post method to send the data so you also have to use post method to receive data. can you please observe the above code and suggest me some solution, the transaction is not in the query string, it's direct because the Android developer sending the image in base64code and it's very large.
C#
string firstname = HttpContext.Current.Request["firstname"];


or

string firstname = Request.Form["firstname"];
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900