R.Vector
R.Vector

Reputation: 1719

Pass parameter to a page

I am good in C# but I haven't yet worked with ASP.NET. I would like to pass a parameter to a page and the page will print it to the user. I'm doing the following in my application to pass parameters of type POST

WebRequest request = WebRequest.Create("http://www.website.com/page.aspx");
request.Method = "POST";
string post_data = "id=123&base=data";
byte[] array = Encoding.UTF8.GetBytes(post_data);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = array.Length;

Now that I have passed the parameters how can I access them from my page? Also, is my above method is correct for asp.net posting? I tried that with PHP and that worked fine.

Upvotes: 2

Views: 3801

Answers (2)

Christophe Geers
Christophe Geers

Reputation: 8952

To post the data:

var request = (HttpWebRequest) WebRequest.Create("http://www.website.com/page.aspx");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

var postData = Encoding.UTF8.GetBytes("id=123&base=data");
request.ContentLength = postData.Length;

using (var requestStream = request.GetRequestStream())
{
    requestStream.Write(postData, 0, postData.Length);
}

To read the posted data on the ASP.NET project:

var id = Int32.Parse(Request.Form["id"]);
var data = Request.Form["base"];

Upvotes: 1

The Jonas Persson
The Jonas Persson

Reputation: 1746

In the code-behind on your aspx page, just write

string id = Request.Form["id"].ToString();

if it's posted data, and

string id = Request.Querystring["id"].ToString();

if the data is in the URL

Upvotes: 2

Related Questions