使用JavaScript禁用ASP.NET 1.1验证程序 - 在Firefox中不起作用

时间:2010-09-03 23:13:15

标签: javascript asp.net firefox validation asp.net-1.1

以下尝试适用于IE8但不适用于Firefox(不能使用JQuery):

case 'Template:templateControl:residenceRBL2': 
    if (selected.value == 'Within USA') 
    {
        /* Enable zip textbox and validator */
 document.getElementById("Template_templateControl_zipTxt1").disabled=false;
 ValidatorEnable(document.getElementById('<%=firstPersonZipReqVal.ClientID%>'),
            true);
 ...
    }
    else if (selected.value == 'Outside USA') 
    {
        /* Disable zip textbox and validator */
 document.getElementById("Template_templateControl_zipTxt1").disabled=true;
 ValidatorEnable(document.getElementById('<%=firstPersonZipReqVal.ClientID%>'),
            false);
 ...
    }
    break;

    <asp:Label ID="firstPersonZipLabel" Runat="server"></asp:Label><br />
 <asp:TextBox ID="zipTxt1" Height="19" Width="100" 
        Runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="firstPersonZipReqVal" 
        ControlToValidate="zipTxt1" Display="Dynamic" 
            ErrorMessage="First Person Zip" 
                Runat="server">*</asp:RequiredFieldValidator>

问题描述:根据radiobuttonlist选项,对文本框禁用/重新启用所需的验证程序。基本上,如果他们的地址在美国以外,那么zip文本框验证器将被禁用。在Firefox中,这根本不起作用。

更新1:09-07-2010 我在Firefox中禁用了文本框;我使用的是name属性而不是id。我现在唯一的问题是如何在JS中访问ASP.NET验证器控件的“ClientID”?

更新2:09-07-2010 根据{{​​3}},我认为我可以这样做:

ValidatorEnable(firstPersonZipReqVal, false);

不幸的是,这似乎在Firefox中不起作用......

MSDN documentation

更新3:09-08-2010

Firefox没有出色的原因是因为ASP.NET 1.1将其视为低级浏览器。如果我在Page标签中放置了clientTarget =“upLevel”,Firefox就会按预期工作。不幸的是,这打破了整个网站布局。 是否有更为渐进的方法来修复Firefox的浏览器? ASP.NET broken rendering of Validation client side code in Firefox的browserCaps也会破坏布局。

Web.Config中的当前browserCaps看起来像这样:

 <browserCaps> 
  <case match="Gecko/[-\d]+">
   browser=Netscape
   frames=true
   tables=true
   cookies=true
   javascript=true
   javaapplets=true
   ecmascriptversion=1.5
   w3cdomversion=1.0
   css1=true
   css2=true
   xml=true
   tagwriter=System.Web.UI.HtmlTextWriter
   <case match="rv:1.0[^\.](?'letters'\w*)">
    version=6.0
    majorversion=6
    minorversion=0
    <case match="^b" with="${letters}">
     beta=true
    </case>
   </case>
   <case match="rv:1(\.\d+)(\.\d)?(?'letters'\w*)">
    version=7.0
    majorversion=7
    minorversion=0
    <case match="^b" with="${letters}">
     beta=true
    </case>
   </case>
  </case>
 </browserCaps>

更新时间:09-11-2010

以下链接可提供答案;有人可以协助50分的代码吗?

This version

5 个答案:

答案 0 :(得分:1)

  

如何访问“ClientID”   JS中的ASP.NET验证器控件

您在代码中以正确的方式访问它,但您可能需要使用双引号而不是单引号:

document.getElementById("<%=firstPersonZipReqVal.ClientID%>")

答案 1 :(得分:0)

这太长了,无法添加到评论中,并且是对我上述帖子的答案的单独抨击。

我直接从a post about this issue复制并粘贴此内容,但是用户尝试过这样的内容:

function disableValidator(elem)
{
    elem.style.cssText = "";
    elem.style.display = 'none';
    elem.style.accelerator = true;
}

disableValidator( document.getElementById("<%=firstPersonZipReqVal.ClientID%>") );

答案 2 :(得分:0)

我认为这里的问题是您在DOM准备好之前调用Javascript ,这就是为什么找不到带有getElementByID的元素。

有两种解决方案。

  1. 渲染您的JavaScript 位于页面底部,至少在您的控件之后
  2. 将您的Javascript调用放在函数上,然后先使用 window.onload 调用它。
  3. 当然,您需要使用&lt;%= firstPersonZipReqVal.ClientID%&gt;

    使用palswim建议调用它们

    我不是100%这是问题,因为我没有看到完整的代码,但这是getElementByID找不到现有控件的主要原因。

答案 3 :(得分:0)

标记为答案 - 请参阅Scott Mitchell关于上述内容的链接。

答案 4 :(得分:0)

好吧,问题很老,但是fr1.1还活着 - 实际上Mitchell的文章没有提供解决方法,让我向我展示:
1. Web配置browserCaps应包含“msdomversion”,如下所示:

        <browserCaps>
        <case match="^Mozilla/5\.0 \([^)]*\) (Gecko/[-\d]+)(?'VendorProductToken' (?'type'[^/\d]*)([\d]*)/(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*)))?">
            browser=Gecko
            <filter>
                <case match="(Gecko/[-\d]+)(?'VendorProductToken' (?'type'[^/\d]*)([\d]*)/(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*)))">
                type=${type}
                </case>
                <case>
                    <!-- plain Mozilla if no VendorProductToken found -->
                type=Mozilla
                </case>
            </filter>
            frames=true
            tables=true
            cookies=true
            javascript=true
            javaapplets=true
            ecmascriptversion=1.2 
            w3cdomversion=1.0
            css1=true
            css2=true
            xml=true
            msdomversion=6.0
            tagwriter=System.Web.UI.HtmlTextWriter
            <case match="rv:(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*))">
                version=${version}
                majorversion=0${major}
                minorversion=0${minor}
                <case match="^b" with="${letters}">
                    beta=true
                </case>
            </case>
        </case>
        <case match="Chrome/(?'version'(?'major'\d+)\.(?'minor'\d+\.\d+).\d+)">
            browser=Chrome
            version=${version}
            majorversion=${major}
            minorversion=${minor}
            frames=true
            tables=true
            cookies=true
            javascript=true
            javaapplets=true
            ecmascriptversion=1.5
            w3cdomversion=1.0
            msdomversion=6.0
            css1=true
            css2=true
            xml=true
            tagwriter=System.Web.UI.HtmlTextWriter
        </case>
    </browserCaps>

2。应修改WebUIValidation.js:
2.1用document.getElementById(Mitchell)替换对document.all的调用 2.2重命名变量“final”(米切尔)
2.3添加函数读取expando作为属性:

function ValidatorValidatorGetAttribute(item, attribName)
{
    var retVal = null;
    var attribs = item.attributes;
    for (var i = attribs.length - 1; i >= 0; i--)
    {
        var attrib = attribs[i];
        if (attrib.nodeName == attribName)
        {
            retVal = attrib.nodeValue;
        }
    }
    return retVal;
}

2.4添加expando处理逻辑,例如在ValidatorOnLoad:

if (typeof(val.evaluationfunction) == "string") {
    eval("val.evaluationfunction = " + val.evaluationfunction + ";");
}
else
if (typeof(ValidatorGetAttribute(val,'evaluationfunction')) == "string") {
    eval("val.evaluationfunction = " + ValidatorGetAttribute(val,'evaluationfunction') + ";");
}

3。在页面上添加客户端脚本(自动脚本检查它是否仅在IE浏览器上运行)

var nonIEBrowser = false;
if (typeof(clientInformation) == "undefined")
    nonIEBrowser = true;
if ((typeof(clientInformation) != "undefined") && (clientInformation.appName.indexOf("Explorer") == -1))
    nonIEBrowser = true;
//None - IE browsers, and possibly IE 10+ 
if (nonIEBrowser) 
{
    if (typeof(Page_ValidationVer) == "undefined")
        alert("Unable to find script library '/aspnet_client/system_web/1_1_4322/WebUIValidation.js'. Try placing this file manually, or reinstall by running 'aspnet_regiis -c'.");
    else if (Page_ValidationVer != "125")
        alert("This page uses an incorrect version of WebUIValidation.js. The page expects version 125. The script library is " + Page_ValidationVer + ".");
    else
        ValidatorOnLoad();
}

就是这样。至少它对我有用:)