2014-10-06 173 views
0

從驗證示例工作:使用asp.net http://www.w3schools.com/aspnet/showaspx.asp?filename=demo_validationsum驗證一個必填字段

我想顯示錯誤消息「您必須在以下字段中輸入一個值:」包含的字段名稱錯誤地輸入數據時是錯誤的。

到目前爲止,用戶無法繼續,直到輸入正確的數據並在列旁邊出現紅色*。我現在想添加錯誤消息。

<div id="floater_EditData_Panel_popup" title="Feature Information" class="floaterDiv"> 
Select a feature to view/edit feature information. 
<p>&nbsp;</p> 
    <asp:UpdatePanel runat="server" id="UpdatePanelDetails1" updatemode="Conditional" Childrenastriggers="false"> 
    <ContentTemplate> 
     <asp:PlaceHolder ID="PlaceHolder1" runat="server" > 
     </asp:PlaceHolder> 
     <br /> 
     <br /> 
     <div id="EditDataPanelMessageOutput" style="color:Red;"> 

      <asp:ValidationSummary ID="ValidationSummary1" 
     HeaderText="You must enter a value in the following fields:" 
     DisplayMode="BulletList" 
     EnableClientScript="true" 
     runat="server"/> 

     </div> 
     <div id="FeatureMeasureOutput"></div> 
     <br /> 
     <br /> 
     <div class="buttonwrap"> 
     <div id="Span2" class="ActionBtns" style="display: inline-block;" > 
      <asp:button runat="server" id="UpdateButton" value="Save" Text="Save" OnClientClick="ValidateEditData();" CausesValidation="true" ValidationGroup="g_currentSelectedLayerName" /> 

     </div> 
     </div> 
    </ContentTemplate> 
    </asp:UpdatePanel> 
</div> 


    function ValidateEditData() { 
      if (Page_ClientValidate(g_currentSelectedLayerName)) { 
       //alert('it is valid'); 
       updateFeature(); 
       return true; 
      } 
      else { 
       //alert('Data not valid'); 
       return false; 
      } 
     } 

不應該驗證摘要標籤應用到整個頁面?

+0

你這是什麼意思'包括被wrong'作爲W3Schools的例子是字段名已經在做 – 2014-10-06 12:31:53

+0

是的,但它在w3school的例子中工作,因爲我的工作不正常,也沒有顯示錯誤信息 – John 2014-10-06 12:53:53

+1

你想驗證什麼(輸入控件)?您的代碼中沒有任何輸入控件 – 2014-10-06 13:18:17

回答

0

如何:

<asp:ValidationSummary ID="ValidationSummary1" 
    HeaderText="You must enter a value in the following fields:" 
    DisplayMode="BulletList" 
    EnableClientScript="true" 
    ValidationGroup="g_currentSelectedLayerName" 
    runat="server"/> 

    <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox> 

    <asp:RequiredFieldValidator runat="server" Display="Dynamic" ControlToValidate="txtFirstName" ErrorMessage="First Name is required" ValidationGroup="g_currentSelectedLayerName"></asp:RequiredFieldValidator> 

    <asp:Button runat="server" Text="Submit" ValidationGroup="g_currentSelectedLayerName" /> 

然後一個appSetting添加到您的web.config:

<appSettings> 
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" /> 
</appSettings> 
+0

我認爲將ValidationGroup添加到所有驗證器控件並將ErrorMessage屬性添加到您的驗證器可以完成這項工作:) – 2014-10-06 19:28:56