冲突的ScriptManager用的CustomValidator客户端验证功能 [英] ScriptManager Conflicting with CustomValidator Client Validation Function

查看:135
本文介绍了冲突的ScriptManager用的CustomValidator客户端验证功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关清晰起见,认为是基于母版页文件,下面的表格。

表格

 <%@页面语言=C#AutoEventWireup =真codeFILE =Test.aspx.cs继承=Secured_Test的MasterPageFile =〜/ MasterPages /Test_SiteMaster.master%GT;< ASP:内容=服务器ContentPlaceHolderID =MainPageContent>  <脚本类型=文/ JavaScript的>
    功能ClientValidate(来源参数)
    {
      警报('富');
    }
  < / SCRIPT>  < ASP:DropDownList的=服务器ID =DDL>
    < ASP:ListItem的文本=值1VALUE =1/>
    < ASP:ListItem的文本=值2VALUE =2/>
  < / ASP:DropDownList的>  < ASP:错误的CustomValidator ID =CustomValidator1=服务器文本= ClientValidationFunction =ClientValidate/>  < ASP:按钮的ID =Button1的=服务器文本=提交的CausesValidation =真/>< / ASP:内容>

我的母版页包含以下code:

 < ASP:的ScriptManager ID =ToolkitScriptManager1=服务器>
  <脚本和GT;
    < ASP:的ScriptReference NAME =jQuery的/>
  < /脚本>
< / ASP:ScriptManager的>

问/问题:

由于是, ClientValidate 函数永远不会触发。如果我删除我的MasterPageFile使用ScriptManager调用,code运行正常( ClientValidate 被调用,并单击按钮时发出警报)。谢谢你在前进!


解决方案

 如果(!Page.ClientScript.IsClientScriptBlockRegistered(办法)){
字符串脚本= @
   功能ClientValidate(来源参数)
   {
      警报('富');
   };
ScriptManager.RegisterClientScriptBlock(this.Page,this.GetType(),办法,剧本,真)
}

但首先你必须检查你的浏览器的错误控制台。也许已经被加载脚本scriptreferrence抛出一个错误和所有其它脚本的执行被停止。

For the clarity's sake, consider the following form that is based on a master page file.

Form:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Secured_Test" MasterPageFile="~/MasterPages/Test_SiteMaster.master" %>

<asp:Content runat="server" ContentPlaceHolderID="MainPageContent">

  <script type="text/javascript">
    function ClientValidate(source, args) 
    {
      alert('foo');
    }
  </script>

  <asp:DropDownList runat="server" id="ddl">
    <asp:ListItem Text="value 1" Value="1" />
    <asp:ListItem Text="value 2" Value="2" />
  </asp:DropDownList>

  <asp:CustomValidator ID="CustomValidator1" runat="server" Text="Error!" ClientValidationFunction="ClientValidate" />

  <asp:Button ID="Button1" runat="server" Text="Submit" CausesValidation="true" />

</asp:Content>

My master page contains the following code:

<asp:ScriptManager ID="ToolkitScriptManager1" runat="server">
  <Scripts>
    <asp:ScriptReference Name="jquery" />
  </Scripts>
</asp:ScriptManager>

Question / Issue:

As is, ClientValidate function never fires. If I remove the ScriptManager call in my MasterPageFile, the code works as expected (ClientValidate is called and and an alert is issued when the button is clicked). Thank you in advance!

解决方案

if(!Page.ClientScript.IsClientScriptBlockRegistered("method")){
string script=@"
   function ClientValidate(source, args) 
   {
      alert('foo');
   }";
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),  "method", script, true)
}

But in first you have to check error console of your browser. Maybe script which have been loaded by scriptreferrence throw an error and all other scripts execution were stopped.

这篇关于冲突的ScriptManager用的CustomValidator客户端验证功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆