【问题标题】:escaping html character strings from Dropdown list box with datasource使用数据源从下拉列表框中转义 html 字符串
【发布时间】:2026-02-11 07:00:02
【问题描述】:

我继承了一个正在迁移到网络的数据库。问题是一些字符串字段有html字符....

一个例子是......

"" 没有引号,它不会出现在这里,尝试别的东西。无论如何,它有 符号,中间有文字。

如何避免任何可能的 html 引用组合。

SelectCommand="usp_getSingleStringData" SelectCommandType="StoredProcedure" 
<SelectParameters>
    <asp:SessionParameter Name="shrtText" SessionField="shrtText" Type="String" />
     <asp:SessionParameter Name="tableName" SessionField="currUserTable" Type="String" />
</SelectParameters>




Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

    If DropDownList1.SelectedValue = "Select" Then
        GridView1.Visible = False
        Exit Sub
    End If
    GridView1.Visible = True

    Dim TestString As String = DropDownList1.SelectedValue
    Dim EncodedString As String = Server.HtmlEncode(TestString)

    Session("shrtText") = EncodedString

End Sub

错误信息是....

从客户端检测到具有潜在危险的 Request.Form 值 (ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder2$DropDownList1="")。

也试过 Texbox,

从客户端检测到有潜在危险的 Request.Form 值 (ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder2$GridView3$ctl17$TextBox1="")。

感谢您的帮助

【问题讨论】:

    标签: asp.net vb.net stored-procedures


    【解决方案1】:

    如何将每个字符转换为 html 编码的表示形式。

    Hello World!
    %48%65%6C%6C%6F$20%57%6F%72%6C%64%21
    
    我知道它的工作量很大,但是这样做可以吗?

    【讨论】:

    • 写一个 SQL 函数为你转换它。
    【解决方案2】:

    这解决了问题...

    newLangText = HttpUtility.HtmlDecode(row.Cells.Item(colCounter).Text)
    

    感谢您的意见,它让我一直在寻找。

    【讨论】: