我想知道在两个asp.net页面之间传递值(有时是多个值)的最佳实践
在过去,我使用查询字符串在asp中传递一个值,如下所示:
href='<%# Eval("TestID","../Net/TestPage.aspx?TestID={0}") %>'><%#Eval("Title")%> </a>
Run Code Online (Sandbox Code Playgroud)
我假设您可以在后面的代码中执行此操作,但我不知道最好的方法.
我还假设可以传递多个值.
有人可以给我一个VB片段,让我知道如何解决这个问题吗?
您有许多传递数据的选项,所有这些选项都可以在页面之间传递多个值.
您可以使用Request.Form集合捕获已使用POST动词从HTML表单提交的值(即"method ="POST">).
代码看起来像:
Dim formvalue As String
formValue = Request.Form("FormField1")
Run Code Online (Sandbox Code Playgroud)
您还可以在URL 查询字符串中使用参数(非常类似于您的示例):
Dim queryStringValue As String
queryStringValue = Request.QueryString("QueryStringValue1")
Run Code Online (Sandbox Code Playgroud)
您可以设置一个cookie(它的生命周期取决于您设置的Expiry属性值):
设置cookie(注意:你在这里使用HttpResponse对象.用户的浏览器在收到来自对请求的响应的Set-Cookie HTTP头值时存储cookie)
Response.Cookies("CookieValue") = "My Cookie Data"
Response.Cookies("CookieValue").Expires = DateTime.Now.AddDays(1) ' optional, expires tomorrow
Run Code Online (Sandbox Code Playgroud)
检索cookie值(我们在这里使用HttpRequest对象):
Dim cookieValue As String
cookieValue = Request.Cookies("CookieValue")
Run Code Online (Sandbox Code Playgroud)
您可以使用HttpSessionState对象(可通过页面的Session属性访问).要设置会话变量:
Session["SessionValue"] = "My Session Value"
Run Code Online (Sandbox Code Playgroud)
要检索会话值:
Dim sessionValue As String
sessionValue = Session["SessionValue"]
Run Code Online (Sandbox Code Playgroud)
还有另一种方法可以在页面之间传递页面状态Page.Transfer(请参阅如何:在ASP.NET网页之间传递值),但在调查之前我会尝试使用上述内容.
就最佳实践而言,它实际上取决于您传递的数据.