加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 在WebControl中使用`<%=`是不错的主意

发布时间:2020-12-16 01:37:54 所属栏目:百科 来源:网络整理
导读:最近我开始在我的Web控件中使用%=.通常我会在Code Behind中设置String属性,然后将它们吐出到表单上. 这是一个坏主意吗? 例如. 代码背后: 属性: public string TheTitle { get; set; }public string TheBody { get; set; }public ContentItem TheContent
最近我开始在我的Web控件中使用<%=.通常我会在Code Behind中设置String属性,然后将它们吐出到表单上. 这是一个坏主意吗? 例如. 代码背后: 属性:

public string TheTitle { get; set; }
public string TheBody { get; set; }
public ContentItem TheContent { get; set; }
public string ContentId { 
  get 
    { return "content" + (TheContent != null) ? TheContent.Id.ToSTring() : "0"; }
}

Page_Load中:

TheTitle = TheContentItem.Title;
TheBody = TheContentItem.Body;

在页面上:

<div id='<%= ContentID %>'>    

  <h2 class='title'><%= TheTitle ?? "No Title" %></h2>
  <p><%= TheBody %></p>

</div>

解决方法

只有在未验证数据时才会出现问题.

使用.NET 4<%:TheBody%>语法是编码可能不受信任的数据的有效方法.在早期版本的框架中,您可以使用<%= HttpUtility.HtmlEncode(TheBody)%>达到同样的效果.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读