1

My code below:

Test.aspx:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <input type="text" value="<%=HttpUtility.HtmlEncode(ab)%>" runat="server"/>
    </form>
</body>
</html>

Test.cs:

 public partial class Test: System.Web.UI.Page
    {
       public string ab;
        protected void Page_Load(object sender, EventArgs e)
        {
             ab = "<script>alert('111');</script>";
        }
    }

After running the test.aspx page,the textbox value is <%=HttpUtility.HtmlEncode(ab)%>

But remove the runat="server" string show correct!

4

1 に答える 1

2

コントロールに対してを実行するrunat="server"と、従来の HTML タグではなくサーバー コントロールになるため、属性はサーバー上で処理されます。残念ながら、これは、インライン スクリプト タグが常に希望どおりに機能するとは限らないことを意味します。

いくつかのことができます。あなたが言ったようにそのままにしておくrunat="server"と、必要に応じて正確にレンダリングされるか、コードに値を設定します。

myTextBox.Value = "whatever";

データ バインディングを使用することもできますが、ちょっと見にくいです。

<input id="myTextBox" runat="server" type="text"
    value='<%# HttpUtility.HtmlEncode("someString") %>' />

protected void Page_Load(object sender, EventArgs e) {
    myTextBox.DataBind();
}
于 2013-07-05T18:45:59.653 に答える