ASP.NET C#表单验证 [英] ASP.NET c# Form validation

查看:104
本文介绍了ASP.NET C#表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有禁用所有验证程序的ASP.NET Web窗体。我想启用验证只对那些可见的用户后提交按钮被点击控件。

下面是我的功能:

 保护无效Submit_Click(对象发件人,发送System.EventArgs){
    如果(ddlSite.Visible){
        rfvSite.Enabled = TRUE;
        base.Validate();
    }
    其他{
        rfvSite.Enabled = FALSE;
    }

以上code是大多数控件的正常工作。但我有带显示器设置为none一些控件。我只能做,如果某些选项是由它们可见。这些无形的控制是造成问题。例如。该pnlOpv面板包含有两个验证revOpv和rfvOpv。一个文本框

 如果(pnlOpv.Visible){
    revOpv.Enabled = TRUE;
    rfvOpv.Enabled = TRUE;
}
其他{
    revOpv.Enabled = FALSE;
    rfvOpv.Enabled = FALSE;
}

以上code是给我的问题,因为即使是因为它包含无形的面板内这上面的文本框是不可见的。对于一些奇怪的原因,表单验证想启用,并会返回页面无效。你看到任何错误?

 < ASP:面板ID =pnlOpv的风格=保证金:0像素;填充:0像素,显示:无; =服务器>
    <标签=txtOpooID =OPO的风格=显示:内联;的margin-top:为5px;><强>参观&LT的其他地方; / STRONG>< /标签>
    < ASP:文本框类型=文本ID =txtOpv的tabindex =2大小=20MAXLENGTH =50=服务器的风格=显示:内联;的margin-top:5像素;背景 - 颜色:#FCFCFC的EnableViewState =真/>
    < ASP:RegularEx pressionValidator ID =revOpv=服务器
        的ControlToValidate =txtOpv
        ValidationEx pression =^ [A-ZA-Z0-9'-'- ,. \\ S] {1,50} $
        显示=动态
        字体名称=宋体FONT-SIZE =10PT启用=假EnableClientScript =真的EnableViewState =真>无效的格式。
    < / ASP:RegularEx pressionValidator>
    < ASP:的RequiredFieldValidator ID =rfvOpv=服务器
        的ControlToValidate =txtOpv
        显示=动态
        字体名称=宋体FONT-SIZE =10PT启用=假的EnableViewState =真EnableClientScript =真>
        请键入其他地方访问。
    < / ASP:&的RequiredFieldValidator GT;
< / ASP:面板>


解决方案

,该小组在样式的事实显示:无并不意味着它是不可见的。对于ASP.Net视图状态的面板,它的一切是可见的。只有当你设定的ASP.Net属性可见=假这将是不可见的。

现在,对于那些你隐藏/带造型和JavaScript(我猜的)我会建议检查在运行时的属性和火基于它的验证,虽然我怀疑显示控件的属性不会包含任何引用样式(再次显示:无)。

我怀疑这就是射击的验证和检查那些可见/不可见时,是什么导致了你的问题。

让我知道这是否有助于或有其他任何问题!

I have an ASP.NET web form with all validators disabled. I want to enable validators only for those controls that are visible to user AFTER Submit button is clicked.

Here is my function:

protected void Submit_Click(object sender, System.EventArgs e) {
    if (ddlSite.Visible){    
        rfvSite.Enabled = true;
        base.Validate();
    }
    else {
        rfvSite.Enabled = false;
    }

The above code is working fine for most of the controls. But I have a couple of controls with display set to none. I make them visible only if certain selections are made. These invisible controls are causing problem. E.g. The pnlOpv panel contain a textbox which has two validators revOpv and rfvOpv.

if (pnlOpv.Visible){
    revOpv.Enabled = true;
    rfvOpv.Enabled = true;
}
else {
    revOpv.Enabled = false;
    rfvOpv.Enabled = false;
}

The above code is giving me problem because even though this above textbox is not visible because it is contained inside invisible panel. For some strange reason, the form thinks validators are enabled and would return page as invalid. Do you see any errors?

<asp:panel id="pnlOpv" style="margin:0px; padding:0px; display: none;" runat="server">                            
    <label for="txtOpoo" id="opo" style="display:inline; margin-top:5px;"><strong>Other Place of Visit</strong></label>
    <asp:TextBox type="text" id="txtOpv" tabindex="2" size="20" maxlength="50" runat="server" style="display:inline; margin-top:5px; background-color:#FCFCFC" EnableViewState="true" />
    <asp:RegularExpressionValidator ID="revOpv" runat="server" 
        ControlToValidate="txtOpv"
        ValidationExpression="^[a-zA-Z0-9''-'-,.\s]{1,50}$"
        Display="Dynamic"
        Font-Names="verdana" Font-Size="10pt" Enabled="false" EnableClientScript="true" EnableViewState="true"> Invalid format.
    </asp:RegularExpressionValidator>
    <asp:RequiredFieldValidator id="rfvOpv" runat="server"
        ControlToValidate="txtOpv"
        Display="Dynamic"
        Font-Names="Verdana" Font-Size="10pt" Enabled="false" EnableViewState="true" EnableClientScript="true">
        Please type other place of visit.
    </asp:RequiredFieldValidator>                                
</asp:panel> 

解决方案

The fact that the panel has in the style "display: none" doesn't mean that it's invisible. For the ASP.Net viewstate the panel and everything on it is visible. It will be invisible only if you set the ASP.Net Property "visible=false".

Now, for those controls that you're hiding/showing with styling and javascript (I guess) I would suggest to check the attributes at runtime and fire the validations based on it, although, I suspect that the attributes won't contain anything referring to the style (again, display: none).

I suspect that's what's causing you problems when firing the validations and checking for those that are visible/not visible.

Let me know if that helps or have any other questions!

这篇关于ASP.NET C#表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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