2

我有一个使用 cc1:Editor 的文本框,当我在其上放置必填字段验证器时,它会在页面加载后立即显示。如何隐藏错误并仅在 html 编辑器框为空时才显示?

<code><cc1:Editor ID="txtDescription" Width="500px" Height="525px" 
 runat="server" TextMode="SingleLine" /></code>
<code>
<asp:RequiredFieldValidator ID="rfvDescription" runat="server" 
ControlToValidate="txtDescription"  
ErrorMessage="Must include Description" Font-Bold="True"></asp:RequiredFieldValidator>
</code>

<code><asp:Button ID="SubmitBtn" runat="server" 
Text="Submit" OnClick="SubmitBtn_Click" class="form-td" Height="30px" />

<script language="javascript" type="text/javascript">
    document.onkeypress = keyhandler;
    function keyhandler(e) {
      Key = window.event.keyCode; if (Key == 13) {
        var obj = document.getElementById('<%=SubmitBtn.ClientID%>');
        obj.focus();
        obj.click();
        }
    }
</script>
4

1 回答 1

0

是的,您可以在回发事件的提交按钮客户端和服务器端验证上执行自定义验证,从而删除必需的字段验证器。检查以下示例:在您的 aspx 页面上

<p>

    <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="javascript:return ValidateEditor();" 
        ValidationGroup="myGroup" onclick="Button1_Click" />
    <br />
</p>
<asp:Label ID="lblMessage" runat="server" ForeColor="Red"></asp:Label>
<cc1:Editor ID="Editor1" runat="server" />
<script type="text/javascript">
    function ValidateEditor() {
        var txtEditor = $find('<%=Editor1.ClientID %>');
        if (txtEditor.get_content() == null || txtEditor.get_content() == '') {
            alert('Text cannot be empty');
            document.getElementById('<%=lblMessage.ClientID %>').innerHTML='Text cannot be empty';
            return false;
        }
        else
            return true;
    }
</script>

在服务器端

 if (string.IsNullOrEmpty(Editor1.Content.ToString()))
        {
            lblMessage.Text = "Text cannot be empty";
        }
        else
        {
            // Do your work here
        }
于 2012-10-09T16:55:43.613 回答