开发者

How do I transfer simple parameter from one ASP.Net page to another?

In the course I'm taking they taught me to use Response.Redirect like this:

Response.Redirect(string.Format("name.aspx?sb="+bts+"&del="+delimiter));

Is there a be开发者_StackOverflowtter way to do so?


Specifically what you are discussing is the means to transfer state between pages. That can be done in an handful of ways:

  1. Query string parameters. This is the equivalent of what you have done in your post.
  2. Session. In this scenario, you would populate a Session variable on one page and then retrieve it on the other.
  3. Cookies.
  4. Form variables if you post directly to the page in question.


See How to: Pass Values Between ASP.NET Web Pages.

In addition you can also use the HttpContext.Current.Items collection to pass data if you are performing a Server.Transfer instead of a Response.Redirect.

Also, the code snippet:

  • Is just performing string concatenation so you don't need to call string.Format.

  • Should UrlEncode query string parameters

e.g.

Response.Redirect("name.aspx?sb=" + Server.UrlEncode(bts) 
    + "&del=" + Server.UrlEncode(delimiter));


There are several different ways to allow information to be sent across multiple pages.

  1. Query String parameters. (like your example)
  2. Session variables
  3. Cookies
  4. form variables (when posting to a different page)

It's really up to you which one to use.

Query string is usually easy, I use those when the landing page is the only thing needing the data.

Session requires you to have a session provider defined and to make some decisions regarding, in process, out of process, etc. Also this will keep the data around until either session expires or you explicitly clear it.

Cookies are good, bear in mind all cookies can be viewed by the client.

Form variables for cross form posting is probably the rarest one and comes with it's own caveats to make sure .net accepts this.


If the link depends on information you know on Page Load, rather than information entered by the user during a postback, I would generate the link upfront.

MyHyperlink.NavigateUrl = "name.aspx?sb="+bts+"&del="+delimiter;

That way the client doesn't have to round-trip to the server to learn what address to redirect to.


As @Thomas mentions, there are a number of ways to do what you're asking.

  1. Query string params: Easy, and should be used if at all possible. You can have multiple tabs/browsers open without affecting each other.

  2. Session. This seems easy, but it falls down when you have the potential for having multiple tabs open, since you only have a single session object. I've seen applications corrupt data: open tab, load object into session. Open 2nd tab, 2nd object replaces 1st object in session. Go back to 1st tab, make a change, click save: boom, 2nd object gets properties of 1st.

  3. I can't think of a single reason for ever using this - all the limitation of the session, with none of the benefits.

  4. Form Variables: You can post to a different page, and there are situation where you'd want to use this, but generally this is going to add complexity where it's not needed, especially in asp.net - viewstate, etc could cause problems.


I'm adding another answer because it's possible everyone here missed your real question.

If the real question is: what is the best way to assemble a string for querystring parameters then you should use:

Response.Redirect(String.Format("name.aspx?sb={0}&del={1}", bts, delimiter));

This allows you to have the string itself constructed and the format method will inject the parameters at the defined positions.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜