how to display a session value in an ASP textbox

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-19 17:42:53

问题


A basic question but there is no such question on Stack Overflow (for ASP.NET)

<asp:TextBox ID="txtUserName" runat="server" Text=<% Session["UserName"] %> >

I did this a week ago, now something is wrong. It should be simple. I also tried <%= %>, it did not work either. Putting a single quote around '<% %>' gives binding error. Help


回答1:


I normally hide the implementation details from the aspx code with a property:

.cs file

public string UserName { get { return Session["UserName"]; } }

.aspx

<asp:TextBox ID="txtUserName" runat="server" Text='<%= UserName %>' >



回答2:


What I did was pulled the text box in C# code and set it text value to the session. Example:

txtUserName.text = Session["UserName"];

Use it in one of the function which checks the session values or you can use in page_load function (the default function for every page)




回答3:


Now I think your code should look like <asp:TextBox ID="txtUserName" runat="server" Text='<%# Session["UserName"] %>' >

I always forget the sintax for inline code, this information could be helpfull I think.



来源:https://stackoverflow.com/questions/6791030/how-to-display-a-session-value-in-an-asp-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!