if you please help me out i am trying to pass 3 different parameters in a page but i am new in asp.net C# and i don't know the correct syntax if you please help me out
;
For one parameter like this it works:
Response.Redirect("~/WebPage2.aspx?q=" + ListBox1.SelectedValue);
how can i write it for 3 parameters like this don't seem to work?
Response.Redirect("~/WebPage2.aspx?q=" + ListBox1.SelectedValue+"&cr="+ListBox3.SelectedValue+"&p="+ListBox1.SelectedValue)
thanks in advance
You forgot a +
in your string concatenations after the cr
parameter. This being said a far safer and better approach which ensures that your parameters are properly encoded is the following:
var parameters = HttpUtility.ParseQueryString(string.Empty);
parameters["q"] = ListBox1.SelectedValue;
parameters["cr"] = ListBox3.SelectedValue;
parameters["p"] = ListBox1.SelectedValue;
var url = string.Format("~/WebPage2.aspx?{0}", parameters.ToString());
Response.Redirect(url);
And of course if you are using ASP.NET MVC (as you've tagged your question with it) you would use:
return RedirectToAction("SomeAction", new {
q = ListBox1.SelectedValue,
cr = ListBox3.SelectedValue,
p = ListBox1.SelectedValue
});
I very sincerely hope that if you are using ASP.NET MVC then ListBox1
and ListBox2
is not what I think it is.
Here's what I typically do (assuming a constant number of parameters for each URL):
string url = "~/WebPage2.aspx?q={q}&cr={cr}&p={p}";
url = url.Replace("{q}", ListBox1.SelectedValue)
.Replace("{cr}", ListBox2.SelectedValue)
.Replace("{q}", ListBox3.SelectedValue);
Response.Redirect(url);
I have not tested it, but this may be relatively inefficient. The reason I do it this way is so I know exactly what the URL pattern looks like and which parameters are used.
It's a trade-off to be sure, and am curious to see other peoples' feedback.
精彩评论