代码改变世界

PeopleEditor允许客户端输入的同时验证输入的内容

2009-12-28 22:43  Virus-BeautyCode  阅读(2489)  评论(9编辑  收藏  举报

我们在开发sharepoint应用的时候,会觉得他的人员选择控件peopleeditor是一个不错的控件,就想要直接的使用它在我们的usercontrol中,但是他本身是不提供验证输入内容的,除非你手动点击控件右下角的【检查名称】图标。有两个办法可以解决,一个就是关闭控件的输入功能

allowtypein="false"

,只让用户通过选择人员来添加人员,但是这样有的用户觉得不能输入太麻烦了;那就打开输入功能

allowtypein="true",这时候就需要在输入完毕之后点击【检查名称】,然后才能【确定】按钮,提交数据,要不然,提交的没有验证,没有反应,没有提示。看起来很不爽啊!!!!!

通过查看页面的源代码,发现在他的【检查名称】图标的a标记的onclick里面有一段javascript代码,是用来实现客户端验证输入的人员是否存在于系统,我就想我们是否可以在【确定】按钮的客户端验证也添加这段脚本,或者让他帮助点击一下那个【检查名称】图标链接呢?这里使用的就是直接在客户端调用那个a标记的onclick方法

<script type="text/javascript">
        function clientvalidate() {

            var client = document.getElementById("<%=peSelectPeople.ClientID %>" + "_checkNames");
            client.click();
            var error = document.getElementById("<%=peSelectPeople.ClientID %>" + "_errorLabel");
            if (error.innerHTML == "" || error.innerHTML==("必须为此必填字段指定值。"))
                return false;
            else
                return true;
        }
</script>

上面是一段JavaScript代码,是一个函数,里面调用了【检查名称】checkNames标记的脚本代码,然后判断errorLabel控件是否提示错误信息。如果提示错误,则返回false;否则返回true。

 

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AddPeopleToDepartment.ascx.cs"
    Inherits="Kimbanx.UCS.SystemMaintenance.AddPeopleToDepartment" %>
<%@ Register Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
    Namespace="Microsoft.SharePoint.WebControls" TagPrefix="cc1" %>

<script type="text/javascript">
        function clientvalidate() {

            var client = document.getElementById("<%=peSelectPeople.ClientID %>" + "_checkNames");
            client.click();
            var error = document.getElementById("<%=peSelectPeople.ClientID %>" + "_errorLabel");
            if (error.innerHTML == "" || error.innerHTML==("必须为此必填字段指定值。"))
                return false;
            else
                return true;
        }
</script>

<table width="90%">
    <tr>
        <td class="ms-formlabel">
            所在部门
        </td>
        <td class="ms-formlabel">
            <asp:Label runat="server" ID="lblDepartment" Text=""></asp:Label>
        </td>
    </tr>
    <tr>
        <td class="ms-formlabel" valign="top">
            选择人员
        </td>
        <td class="ms-formbody">
            <cc1:peopleeditor id="peSelectPeople" runat="server" allowempty="False" allowtypein="true"
                validatorenabled="True" />
            <asp:RequiredFieldValidator ID="RequiredFieldValidatorPe" runat="server" ErrorMessage="不能为空"
                ControlToValidate="peSelectPeople" Display="Dynamic"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="ms-formlabel">
            职务
        </td>
        <td class="ms-formbody">
            <asp:TextBox runat="server" ID="txtPosition"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="ms-formlabel">
            优先级
        </td>
        <td class="ms-formbody">
            <asp:TextBox runat="server" ID="txtPriority"></asp:TextBox>
            <asp:RegularExpressionValidator ID="RegularExpressionValidatorPriority" runat="server"
                ErrorMessage="只能是大于等于0的数字" ControlToValidate="txtPriority" ValidationExpression="^[1-9]\d*|0$"
                Display="Dynamic"></asp:RegularExpressionValidator>
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <td colspan="2" align="right">
            <asp:Button ID="btnAdd" Text="添加" runat="server" CssClass="ms-ButtonHeightWidth"
                OnClientClick="return clientvalidate();" OnClick="btnAdd_Click" />&nbsp &nbsp
            <asp:Button ID="btnCancel" Text="取消" runat="server" CssClass="ms-ButtonHeightWidth"
                OnClick="btnCancel_Click" CausesValidation="False" />
        </td>
        <%--<td></td>--%>
    </tr>
</table>