开发者

C# asp.net parameters cast to a page [closed]

开发者 https://www.devze.com 2023-04-04 16:22 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this开发者_StackOverflow question so that it can be reopened, visit the help center. Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消