【问题标题】:How to add value in Input element using C# code in Aspx page?如何在 Aspx 页面中使用 C# 代码在 Input 元素中添加值?
【发布时间】:2018-07-14 08:48:53
【问题描述】:
如果会话变量的值不为空,我想在input 元素中添加值
<input id="sessionInput" type="hidden" value='<%# Session["UserLogin"] != null ? Session["UserLogin"].ToString() : ""; %>' />
但它不起作用。
错误
) 预期
【问题讨论】:
标签:
asp.net
session
master-pages
【解决方案1】:
最后有一个分号。使用绑定表达式时不需要这些。
<input id="sessionInput" type="hidden" value='<%# Session["UserLogin"] != null ? Session["UserLogin"].ToString() : "" %>' />
如果您希望将值绑定到输入,则必须在页面加载中调用 DataBind();。
【解决方案2】:
使用较新的: 对值进行 html 编码。
<input id="sessionInput" type="hidden" value='<%: Session["UserLogin"] %>' />