简体   繁体   English

将会话或cookie变量传递到ASP.NET中的服务器标签

[英]Passing session or cookie variables to server tags in ASP.NET

This is probably a simple question but, it eludes me, I have a variable that I have set as a code-behind session in another page of an ASP.NET application. 这可能是一个简单的问题,但是,使我难以理解的是,我在ASP.NET应用程序的另一页中有一个已设置为代码隐藏会话的变量。 Let's say it is: Session["UserName"] and I want to pass it from a code behind page to the server tag. 假设它是: Session["UserName"] ,我想将其从页面后面的代码传递到服务器标签。 The server tag looks like this: 服务器标签如下所示:

<asp:SqlDataSource ID="SqlDataSourceDelta" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringAlpha %>" SelectCommand="SELECT [ThingTitle], [ThingSynopsis], [ThingCategory], [ThingType], [ThingUserName] FROM [ThingOfSubmissions] WHERE ThingUsername = <%$ Session["UserName"] %>"

I can't use: <%$ Session["UserName"] %> directly in the server tag as shown above without it generating an error about mal formed server tags. 我不能在上面的服务器标记中直接使用<%$ Session["UserName"] %> ,而不会生成关于格式错误的服务器标记的错误。 So my question is, how can I pass this session variable or any other application variable (non SQL Server variable) into this bit of code? 所以我的问题是,如何将该会话变量或任何其他应用程序变量(非SQL Server变量)传递到这段代码中?

You could just set the property in server-side code ? 您可以只在服务器端代码中设置属性?

SqlDataSourceDelta.SelectCommand = string.Format("SELECT [ThingTitle], 
[ThingSynopsis], [ThingCategory], 
[ThingType], [ThingUserName] 
FROM [ThingOfSubmissions] 
WHERE ThingUsername = '{0}'", Session["UserName"]);

I figured it out...I had to set the value as a Session Parameter in the actual page not in the server-side code as follows: 我知道了...我必须在实际页面中而不是在服务器端代码中将值设置为会话参数,如下所示:

  <asp:SessionParameter
            Name="UserName"
            SessionField="UserName"
            Type="String" />

Next, I used @UserName inside the page server tag code and it worked fine. 接下来,我在页面服务器标记代码中使用了@UserName,它工作正常。

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

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