简体   繁体   English

使用ASP.NET(C#)在不使用QueryString的情况下将变量从页面传递到页面

[英]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. 寻找有关在不使用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. 除非microsoft更新它,否则您可以在一个页面上显示多个表单。

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; 您可以查看使用ASP.NET路由 (可以在没有MVC的情况下完成) - 然后您可以拥有一个包含数据但没有查询字符串的路径; like the paths here (which are actually MVC, but the same logic applies). 就像这里的路径(实际上是MVC,但同样的逻辑适用)。

Re not being able to use forms - you could simply use jQuery or similar to create the forms on the fly. 无法使用表单 - 您可以简单地使用jQuery或类似工具来动态创建表单。

How about ASP.NET session store?? ASP.NET会话存储怎么样?

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. 在ASP.Net页面之间传递数据有四种方法。

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. 使用POST(表单)参数。 These are invisible to web crawlers; 这些对于网络抓取工具是不可见的;

Two of them span multiple pages: 其中两个跨越多个页面:

  • The ASP.Net session; ASP.Net会议;

  • Cookies. 饼干。

I think that what you're looking for is POST (form) parameters. 我认为您正在寻找的是POST(表单)参数。 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 . Ruby on Rails通过提供一种在后台创建表单的通用机制来执行类似的操作, 并允许您传递POST参数而无需实际创建表单 When I eg create a link to http://google.com with method set to POST, it generated the following HTML: 当我创建一个方法设置为POST的http://google.com链接时,它会生成以下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'); 我已经格式化了上面的一点但是,但在HTML页面中,这将显示为一个长字符串.. document.createElement('input'); until the appendChild allows you to set the parameters you want to pass on to the following page. 直到appendChild允许您设置要传递给下一页的参数。

It sounds like you want to use a Cookie. 听起来你想要使用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. 如果信息是安全的,您可以将值存储在数据库中,并将数据库记录的唯一ID存储在cookie中。

If you're simply passing information from one page to another, you can do an HTTP post. 如果您只是将信息从一个页面传递到另一个页面,则可以执行HTTP发布。 If you are doing something like logging a user in, then you'd want to use cookies and/or session state 如果您正在执行诸如登录用户之类的操作,那么您需要使用cookie和/或会话状态

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) ViewState(取决于您如何跨页面重定向)
  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 现在,您可以从上一页获取TextBox的值到GetValue.aspx

  1. In aspx page 在aspx页面中

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

or 要么

  1. In code file 在代码文件中

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用C#给出指向标签的链接并将查询字符串传递给asp.net中的另一个页面 - how to give link to the label and pass querystring to another page in asp.net using c# 从ASP.NET网页使用C#文件 - Using C# file from an ASP.NET web page jQuery在使用ASP.NET C#的母版页中不起作用,并且在不使用母版页的情况下也可以正常工作 - Jquery is not working in Master Page using ASP.NET C# and working fine with without using master page 使用javascript将变量从asp.net页传递到新页 - passing variable from asp.net page to new page using javascript 从asp.net c#使用CSS的页面属性为每个打印页面添加页码 - Add Page number using Page Property of CSS for each printed pages from asp.net c# 将变量从子页面传递到父页面asp.net - Passing a variable from child page to parent page asp.net 如何在不使用c#(asp.net)运行项目的情况下30分钟后自动刷新页面 - how to refresh page after 30 minutes automatically without run the project using c#(asp.net) 如何使用 c# 从 asp.net 中的查询字符串中删除项目? - How can I remove item from querystring in asp.net using c#? 更新页面的一部分以显示“新闻”(不使用数据库) Asp.net C# - Updating a section of a page to show “News” (Without using a database) Asp.net C# 是否可以在不使用C#asp.net离开或重新加载页面的情况下替换浏览器的URL? - Is it possible to replace the browser's URL without leaving or reloading the page using C# asp.net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM