从链接到确认按钮扩展程序的asp文本框中删除HTML标记,而不设置ValidateRequest=“”;false”;

Remove HTML tags from asp text box linked to confirm button extender without setting ValidateRequest="false"

本文关键字:设置 标记 false HTML ValidateRequest 删除 按钮 扩展 确认 链接 程序      更新时间:2023-09-26

当用户输入<>asp.net文本框控件中的符号。在模式弹出窗口中,回发时会引发运行时错误。我知道在过去,这种问题的很多答案都是将ValidateRequest设置为"false",但我不想设置它,因为我担心禁用可能阻止其他攻击的安全功能。

用户希望能够复制和粘贴有时包含<>的电子邮件文本标签,但他们并不介意是否删除这些标签,他们只希望在复制和粘贴表单并提交给服务器处理时自动删除这些标签。

有人能为这类问题提供一个解决方案的例子吗?如果不能按照用户的喜好进行,有哪些替代方案?谢谢

所以我遇到的一个障碍是,我想启动JS的模式弹出按钮链接到一个ajax确认按钮扩展程序,该扩展程序不允许它启动。

我不得不使用emca脚本修改确认按钮扩展程序的行为属性,并在该脚本中包含JS函数。最后,我做了一个检查,以确保扩展器不是null,因为根据情况,我可能会实例化控件,也可能不会实例化控件。

这是我的最终解决方案:

在aspx页面开头的Content标签之后,我放了我的脚本:

    <script type="text/ecmascript">
    Sys.Application.add_load(wireEvents);
    function wireEvents() {
        var behavior = $find("confirmBehavior");
        if (behavior !== null)
        { behavior.add_showing(formatText); }
    }
    function formatText() {
        var newValue = document.getElementById('<%=txtStatusComment.ClientID%>').value.replace(/[<>]+/g, "");
        document.getElementById('<%=txtStatusComment.ClientID%>').value = newValue;
    }
   </script>

然后对于按钮,我做了以下操作:

<asp:Button ID="btnSaveStatus" runat="server" CssClass="button" OnClick="btnSaveStatus_Click" Text="Save" OnClientClick ="formatText();" />
                                <asp:Button ID="btnCancelStatus" runat="server" CssClass="button" OnClick="btnCancelStatus_Click" Text="Cancel" OnClientClick ="formatText();" />
                                <cc1:ConfirmButtonExtender ID="btnSaveStatus_ConfirmButtonExtender" BehaviorID="confirmBehavior" runat="server" DisplayModalPopupID="modalPopupExtender3" Enabled="true" TargetControlID="btnSaveStatus">
                                </cc1:ConfirmButtonExtender>

希望这能帮助到任何有同样问题的人!