0

I have a content page which has some dynamic HTML and some static HTML. This dynamic HTML is assigned to a hidden variable in page load.

Content page HTML

<asp:HiddenField id = "hid"  runat="server"></asp>

CS of content page

protected void Page_Load(object sender, EventArgs e)
{
  hid.Value = node1.InnerText;
}

This content page has master page. Below is its page directive

<%@ Page Title="" Language="C#" MasterPageFile="~/log.Master"
  AutoEventWireup="true" CodeBehind="Log.aspx.cs" Inherits="s.Log"
  ValidateRequest="false" %>`

Below is snippet of Content page

<%@ Page Title="" Language="C#" MasterPageFile="~/log.Master"
  AutoEventWireup="true"  CodeBehind="Log.aspx.cs" Inherits="s.Log"
  ValidateRequest="false" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
  <asp:HiddenField id = "hid"  runat="server"/>
</asp:Content>`

Now when content page loads, static HTML is loaded inside master page, but dynamic contents are rendered outside master page hence shows wrong rendering.

4

1 回答 1

0

关键<asp:HiddenField>是它是隐藏的——因此其中包含的任何信息都应该定义永远不会在页面上呈现。

如果该字段的内容包含任何可能导致其中断的 HTML 代码(例如尚未编码的 HTML 代码)......那么它可能会显示某些内容,但很可能是不正确的。我想这就是你所看到的。

我不完全确定您为什么要使用HiddenField,或者您要对它的内容做什么,但我建议您更改以下行...

hid.Value = node1.InnerText;

和...

hid.Value = Server.HtmlEncode(node1.InnerText);
于 2012-07-20T15:08:01.527 回答