简体   繁体   中英

Passing Variable from page to page using ASP.NET (C#) without using QueryString

Looking for advice on passing variables from page to page without using QueryString. The web crawlers such as google get caught up on the querystrings found in a URL. I'm trying to get away from using it. Is there another suggested method for passing the variables? I've thought about using session variables, but this is just for simply passing the variable from page to page and they won't always be the same. Any ideas?

On another note, I can't do it using forms. I'm using a master page with a form embedded in the master page and also the content pages. Unless microsoft updated it so you can multiple forms on one page.

You could look at using the ASP.NET Routing (which can be done without MVC) - then you can have a path with data but without a query-string; like the paths here (which are actually MVC, but the same logic applies).

Re not being able to use forms - you could simply use jQuery or similar to create the forms on the fly.

How about ASP.NET session store??

You can store stuff into the session on your one page, and retrieve it from there on your second page..

You can add anything that is serializable to the session store, and it's really like a glorified dictionary, eg you can stuff something into it using any string as its key, and retrieve it on the other page using that same key.

See:

There are four ways of passing data between ASP.Net pages.

Two of them are localized to a single page, so having two browser tabs will have different parameters:

  • Using the query string, either as query string parameters (what you don't want) or using the path (ie routing, which is described in a different answer);

  • Using POST (form) parameters. These are invisible to web crawlers;

Two of them span multiple pages:

  • The ASP.Net session;

  • Cookies.

I think that what you're looking for is POST (form) parameters. Using this would require you to change links in pages where you pass these parameters into forms. Ruby on Rails does something like this by providing a generic mechanism that creates a form in the background and allows you to pass POST parameters without actually creating a form . When I eg create a link to http://google.com with method set to POST, it generated the following HTML:

<a href="http://google.com" onclick="
    var f = document.createElement('form');
    f.style.display = 'none';
    this.parentNode.appendChild(f);
    f.method = 'POST';
    f.action = this.href;
    var m = document.createElement('input');
    m.setAttribute('type', 'hidden');
    m.setAttribute('name', 'param1');
    m.setAttribute('value', 'value1');
    f.appendChild(m);
    f.submit();
    return false;">Google.com using POST</a>

I've formatted the above a little but, but in the HTML page this would appear as one long string.. The document.createElement('input'); until the appendChild allows you to set the parameters you want to pass on to the following page.

It sounds like you want to use a Cookie. If the information is secure, you can store the values in a database and store the database record's unique ID in the cookie.

If you're simply passing information from one page to another, you can do an HTTP post. If you are doing something like logging a user in, then you'd want to use cookies and/or session state

Depending on the need, you can also do it with the Application State, as you probably guessing the access is all user/session:

 Application.Lock();
 Entity e = new Entity("mine");
 Application["myVar"] = e;
 Application.UnLock();

You can use one of the following:

  1. Session
  2. Cookies
  3. ViewState (depends on how you are redirecting across the pages)
  4. Database
  5. Disk

Your main page has textbox and button like....

<asp:TextBox ID="input" runat="server"></asp:TextBox>
<asp:Button ID="Bt" runat="server" Text="Button" PostBackUrl="~/Getvalue.aspx" />

Now, you can get the value of TextBox from previous page to GetValue.aspx

  1. In aspx page

<%=Request.Form["input"]%>

or

  1. In code file

TextBox1.Text = Request.Form["input"]; (TextBox1 is in  "GetValue.aspx" )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM