简体   繁体   English

如何在HTML后面的代码中显示存储在会话变量中的值?

[英]How can I display the value stored in my Session Variable in my code behind, in HTML?

What I am trying to achieve is to get my session variable called Session["name"] in my code behind file and display it on the browser using HTML. 我要实现的目标是在代码后的文件中获取名为Session["name"]会话变量,并使用HTML将其显示在浏览器中。

What I had tried initially was to place the session between <%%> tags but that did not work. 我最初尝试的是将会话放置在<%%>标记之间,但这没有用。

This is a line of code from my code behind file: 这是我文件后面的代码中的代码行:

string.Format("Welcome, {0}", Session["name"].ToString().Split(' ')[0]);

What is the best way for me to simply display my welcome message using C# and HTML? 对我来说,使用C#和HTML简单地显示欢迎消息的最佳方法是什么? I saw stuff about JSON but I really don't understand it nor where to start if that is the route id need to take. 我看到了有关JSON的内容,但我真的不了解它,也不清楚从哪里开始(如果那是需要使用的路由ID)。 Please advice. 请指教。

You are already creating a session to store name : 您已经在创建一个会话来存储name:

//Create
Session["name"] = value;

Now to show it on the front end or html page you can use label and set text value of that label to the session value as string 现在将其显示在前端或html页面上,您可以使用label并将该标签的文本值设置为会话值作为字符串

//Set session text to label
Label1.Text = Session["name"].ToString();

Hope this helps 希望这可以帮助

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

相关问题 我如何在aspx页面上显示变量值后面的代码 - How do I display the code behind variable value on my aspx page 如何在 Javascript 中获取会话值,但我的 javascript 代码位于 asp.net 后面的代码中。 我正在尝试在 javascript 中设置变量值 - How to get session value in Javascript but my javascript code is in the code behind in asp.net. I am trying to set the variable value in javascript 如何在我的代码后面的表中添加数据并以html显示? - How do I add data to a table in my code behind and display in html? 如何将我的代码隐藏在 MVVM 中的复选框后面? - How can I wtite my code behind for checkbox in MVVM? 如何将jQuery函数放入我的代码后面? - How can I put a jQuery function into my code behind? 如何从后面的代码访问我的 ViewModel - How can I access my ViewModel from code behind 如何在代码中包装我的用户控件? - How can I wrap my user control in code behind? 如何从存储过程中获取返回值以显示在GridView上? - How can I get my return value from stored procedure to display on GridView? 如何在我的应用程序上显示存储在移动设备中的图像? - How can i display image stored in mobile device on my aplication? 如何在MVC视图中显示存储过程的结果? - How can I display stored procedure results in my MVC view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM