asp.net - 自定义 SharePoint 身份验证表单 - 由于页面刷新而显示错误消息的问题
问题描述
我正在尝试基于默认 .aspx 之一为 SharePoint Server 创建自定义表单身份验证页面:
<%@ Assembly Name = "$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace = "Microsoft.SharePoint.ApplicationPages" %>
<%@ Register TagPrefix = "SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix = "Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix = "asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace = "Microsoft.SharePoint" %>
<%@ Assembly Name = "Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language = "C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="SharePointCustomLogin.Layouts.SharePointCustomLogin.Login" MasterPageFile="errorv15.master" %>
<asp:Content ContentPlaceHolderID = "PlaceHolderPageTitle" runat="server">
Site name
<SharePoint:EncodedLiteral runat="server" EncodeMethod="HtmlEncode" ID="ClaimsFormsPageTitle" Visible="false" />
</asp:Content>
<asp:Content ContentPlaceHolderID = "PlaceHolderMain2" runat="server">
<div id = "SslWarning" style="color: red; display: none">
<SharePoint:EncodedLiteral runat = "server" EncodeMethod="HtmlEncode" ID="ClaimsFormsPageMessage" />
</div>
<asp:Login class="share-point-form" ID="signInControl" FailureText="<%$Resources:wss,login_pageFailureText%>" runat="server" Width="100%">
<LayoutTemplate>
<asp:Label ID = "FailureText" runat="server" />
<asp:TextBox ID = "UserName" autocomplete="off" runat="server" Width="99%" />
<asp:TextBox ID = "password" TextMode="Password" autocomplete="off" runat="server" Width="99%" />
<asp:Button ID = "login" CommandName="Login" Text="<%$Resources:wss,login_pagetitle%>" runat="server"/>
<asp:CheckBox ID = "RememberMe" runat="server" />
</LayoutTemplate>
</asp:Login>
</asp:Content>
提交时的母版页表单:
<form runat="server" onsubmit="if (typeof(_spFormOnSubmitWrapper) != 'undefined') {return _spFormOnSubmitWrapper();} else {return true;}">
当 js 字段为空时,我会显示错误消息。问题是提交表单后,页面更新和错误消息很快就会消失。为了处理这个问题,我需要用我自己的替换默认的 onSubmit,为此我需要知道如何复制原始表单 onSubmit 所做的事情,以及特别是要发布到 SP 服务器的内容。这样我就可以通过显示错误消息来验证数据和响应。
有没有人做过这样的事情?对于此类问题,也许还有另一种很好的解决方案。
解决方案
我找到了一个非常简单的解决方法。提交表单时,错误消息会因为回发而消失。所以要解决这个问题,我们需要只在验证代码没有发现任何错误时才允许提交表单。所以我只是将 onClientClick 添加到一个带有功能的 asp:button 中:
onClientClick="return validateFormFields();
如果验证函数返回错误,则无法提交表单 = 没有回发 = 验证错误消息仍然存在。