Sriram B
Sriram B

Reputation: 691

Passing List<T> as query string

I have a List having country names in the list. And this list needs to be sent as one of the parameters in Response.Redirect(page).

Is there a straight forward way to do this, so that in the recieving page, I can type cast the query string to List and use it.

Thanks, Sriram

Upvotes: 2

Views: 26819

Answers (4)

barbara.post
barbara.post

Reputation: 1661

Better using standard format... No ?

string queryString = "?country=" + string.Join("&country=", yourList);

Upvotes: 4

ahaliav fox
ahaliav fox

Reputation: 2247

sending the string from List

 Response.Redirect("page.aspx?CountryList=" + string.Join(",", (string[])TargetArrayList.ToArray()));

getting the QueryString

string str = Request.QueryString["CountryList"];
    string[] arr = str.Split(',');
    TargetArrayList = arr.ToList();

Upvotes: 1

Arion
Arion

Reputation: 31239

I think you can do this. But if is a list comming from the database better call the function again on the revising page. Otherwise you have to do a dirty solution like this:

List<string> ls=new List<string>{"Sweden","USA"};
Response.Redirect("page.aspx?CountryList="+string.Join(",",ls));

And on the revising page do this:

List<string> ls= QueryString["CountryList"].Split(',').ToList();

But remember there is a limit on how large querystring you can send. So if the list contains many items then you can might reach that upper bound.

or store it in session:

Session["CountryList"]=ls;

And then on the revising page. Do this:

List<string> ls=(List<string>)Session["CountryList"];
Session.Remove("CountryList");

Remember to remove the session when you are done with it. So you do not have dirty values in the session

Upvotes: 8

vishnu
vishnu

Reputation: 147

Depends on how large your country list is. I generally use Session["CountriesNames"] as List<string> to pass such values between webpages in asp.net webforms.

Upvotes: 0

Related Questions