Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get string from url in ASP.NET webforms?

I think my tittle is inaccurate.

When a user clicks on a button I need it to do this:

Response.Redirect("Login.aspx?userid=XX");

How can I get the "userid?" from ?userid. so I can show a page. Like doing "?page=3" and show page 3, in the same page or something.

The Button code is: (just if you need it)

protected void LoginButton_Click(object sender, EventArgs e)
{
    Response.Redirect("Login.aspx");
}

Thanks a lot! Sorry if I didn't ask it good, and sorry for the bad English.

like image 368
Alon M Avatar asked Oct 21 '11 19:10

Alon M


People also ask

What is QueryString in C#?

The QueryString collection is used to retrieve the variable values in the HTTP query string. The HTTP query string is specified by the values following the question mark (?), like this: Link with a query string. The line above generates a variable named txt with the value "this is a query string test".

How do you query parameters in a URL?

Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed. To append query params to the end of a URL, a '? ' Is added followed immediately by a query parameter.

What is request QueryString?

The value of Request. QueryString(parameter) is an array of all of the values of parameter that occur in QUERY_STRING. You can determine the number of values of a parameter by calling Request. QueryString(parameter).


1 Answers

Use Request.QueryString:

First Page Sends them another page with their user id in the url:

Response.Redirect("AfterLogIn.aspx?userid=23");

You then Read it using the below code:

var g = Request.QueryString["userid"] //this value should be 23 now

You could then use this g variable to do any amount of custom things (Hide panels, show controls, etc.)

like image 125
KreepN Avatar answered Sep 21 '22 10:09

KreepN