发布于 2024-07-13 21:25:39 字数 992 浏览 10 评论 0原文

以下 .NET 3.5 代码放置在 aspx 文件中,在加载页面时将触发 JavaScript 错误(对于启用了 JavaScript 的用户):

<noscript>
    <asp:TextBox ID="txt" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="txt_RequiredFieldValidator" runat="server" 
      ControlToValidate="txt"></asp:RequiredFieldValidator>
    <asp:Button ID="btn" runat="server" Text="Button" />
</noscript>

发生错误的原因是 ASP.NET 验证器控件生成的 JavaScript 不包含在下面的第二行代码之前进行空检查:

var ctl00_body_txt_RequiredFieldValidator =
  document.all ?
    document.all["ctl00_body_txt_RequiredFieldValidator"] :
    document.getElementById("ctl00_body_txt_RequiredFieldValidator");

ctl00_body_txt_RequiredFieldValidator.controltovalidate = "ctl00_body_txt";

任何人都可以建议解决方法吗?


Footnote: Why am I doing this? For my non-JavaScript users I am replacing some AJAX functionality with some different UI components, which need validation.

The following .NET 3.5 code, placed in an aspx file, will trigger a JavaScript error when the page is loaded (for users who have JavaScript enabled):

<noscript>
    <asp:TextBox ID="txt" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="txt_RequiredFieldValidator" runat="server" 
      ControlToValidate="txt"></asp:RequiredFieldValidator>
    <asp:Button ID="btn" runat="server" Text="Button" />
</noscript>

The error happens because the JavaScript generated by the ASP.NET validator control does not contain a null check on before the second code line below:

var ctl00_body_txt_RequiredFieldValidator =
  document.all ?
    document.all["ctl00_body_txt_RequiredFieldValidator"] :
    document.getElementById("ctl00_body_txt_RequiredFieldValidator");

ctl00_body_txt_RequiredFieldValidator.controltovalidate = "ctl00_body_txt";

Can anyone suggest a workaround to this?


Footnote: Why am I doing this? For my non-JavaScript users I am replacing some AJAX functionality with some different UI components, which need validation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昵称有卵用 2024-07-20 21:25:39

您应该将以下内容添加到RequiredFieldValidator:

enableclientscript="false"

看到您在

这将强制在服务器端为您(自动)进行验证。

只需确保您调用“Page.IsValid< /a>”在处理响应之前。

请参阅 BaseValidator.EnableClientScript更多细节。

You should add the following to the RequiredFieldValidator:

enableclientscript="false"

Seeing as you are using these in <noscript> tags, there is no point in supplying client side vaildation of the controls - they are only going to display if JS is turned off.

This will force the validation to happen (automatically) on the server side for you.

Just make sure you call "Page.IsValid" before you process the response.

See BaseValidator.EnableClientScript for more details.

锦欢 2024-07-20 21:25:39

javascript 正在寻找 noscript 中包含的元素? AFAIK 没有干净的方法来检测服务器端的脚本支持。

我认为您需要构建一个重定向(如果您不介意验证失败,我已经在标头 noscript 中通过元刷新看到了这一点),以将 noscript 用户推送到“请在页面上打开脚本”或做一些手术来放松验证/控制绑定,这可能需要一些重新发明轮子的工作。 这是 ASP.NET 控制器和视图之间的紧密耦合真正造成影响的领域之一。

The javascript is looking for an element contained in the noscript? AFAIK there's no clean way to detect script support from the server side.

I think you'll need to build in a redirect (I've seen this done with a meta-refresh in a header noscript if you don't mind a validation failure) to push noscript users to a "please turnscript on page" or do some surgery to loosen up the validation/control binding which may take some amount of wheel reinventing. This is one of those areas where asp.net's tight coupling between controller and view really punishes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文