2013-10-24 54 views
1
<td>Type: 

</td> 
    <td> 
    <asp:DropDownList ID="ddltype" runat="server"> 
    <asp:ListItem>---select---</asp:ListItem> 
     <asp:ListItem Text="Setter" Value="1"> 
     </asp:ListItem> 
     <asp:ListItem Text="Getter" Value="2"></asp:ListItem> 
      </asp:DropDownList> 
      </td> 

對於這個下拉列表,我把驗證這樣驗證不在asp.net中觸發?

var ddltype = document.getElementById('<%=ddltype.ClientID%>'); 
     var type = ddltype.options[ddltype.selectedValue].value; 
     if (type == 0) { 
      alert("Please Select setter/getter type."); 
      return false; 
     } 

但不點火。我怎麼寫這個?

+2

爲什麼不使用ASP.NET驗證器? –

+0

什麼是導致JavaScript的火災?它是在一個onclick事件?爲什麼不使用ASP.NET驗證中的構建?最少爲0的'RangeValidator'應該這樣做? – RemarkLima

+1

您選擇的選擇沒有任何價值。做到這一點; 然後它會工作 –

回答

5

你真的應該熟悉ASP.NET驗證器。 Javascript可以被禁用。

<asp:DropDownList ID="ddltype" runat="server" AutoPostBack="true"> 
    <asp:ListItem>---select---</asp:ListItem> 
    <asp:ListItem Text="Setter" Value="1"></asp:ListItem> 
    <asp:ListItem Text="Getter" Value="2"></asp:ListItem> 
</asp:DropDownList><br /> 
<asp:RequiredFieldValidator ID="reqType" runat="server" 
    InitialValue="---select---" 
    ControlToValidate="ddltype" 
    ErrorMessage="Required: Please select a Type" 
    Display="Dynamic" 
    CssClass="blah"> 
</asp:RequiredFieldValidator> 

InitialValue很重要。否則---select---將是一個有效的選擇。

請注意,我還將AutoPostBack="true"添加到DropDownList,也許您希望在用戶選擇某個項目後立即回發。

側面說明:如果你想顯示在一個JavaScript警告消息使用ValidationSummaryShowMessageBox="true"EnableClientScript="true"

+0

OP詢問javascript驗證,但爲什麼你改變他的想法? –

+0

ASP。NET驗證器也可以在客戶端進行驗證(如果需要),也可以在服務器端進行驗證。它也更易於閱讀和維護,然後擺弄(冗餘)js-函數。 –

1

試試這個

var ddltype = document.getElementById('<%=ddltype.ClientID%>').text; 
if (type == "---select---") { 
     alert("Please Select setter/getter type."); 
     return false; 
    } 
+1

此外 - 更好的方式是使用RequiredFieldValidator,而不是編寫自定義js驗證。 RequiredFieldValidator也會在服務器端驗證輸入(例如,如果你的用戶禁用了javascript)。 –

+0

是的,同意。但有些需求像這樣的客戶端驗證。 – MusicLovingIndianGirl

+0

我認爲這個OP代碼是正確的!你會錯的! –

-1

試試這個方法!但你可以使用asp.net驗證控件。

任何方式,我的解決方案將驗證兩點式,在下拉菜單中選擇淡水河谷或下拉列表中選擇的項目

function Validate() 
    { 
    var e = document.getElementById('<%=ddltype.ClientID%>'); 
    //if you need value to be compared then use 
    var strUser = e.options[e.selectedIndex].value; 
    //if you need text to be compared then use 
    var strUser1 = e.options[e.selectedIndex].text; 
    if(strUser==0) **//for text use if(strUser1=="---Select---")** 
    { 
    alert("Please Select setter/getter type."); 
     return false; 
    } 
    } 

下面的代碼已經改變你的一些代碼和工作對我好

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <script type="text/javascript"> 
     function validation() { 
      debugger; 
      var e = document.getElementById('<%=ddltype.ClientID%>'); 
      var strUser1 = e.options[e.selectedIndex].value; 
      if (strUser1 == 0) { 
       alert("Please Select setter/getter type."); 
       return false; 
      } 
     } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <table> 
      <tr> 
       <td> 
        <asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="validation();" OnClick="btnSave_Click" /> 
        <asp:DropDownList ID="ddltype" runat="server"> 
         <asp:ListItem Text="--Select--" Value="0"></asp:ListItem> 
         <asp:ListItem Text="Setter" Value="1"> 
         </asp:ListItem> 
         <asp:ListItem Text="Getter" Value="2"></asp:ListItem> 
        </asp:DropDownList> 
       </td> 
      </tr> 
     </table> 
     <div> 
     </div> 
    </form> 
</body> 
</html> 

,看到這一行

<asp:ListItem Text="--Select--" Value="0"></asp:ListItem> 

但是你錯過了該項目的設置值,所以它出現錯誤,現在在該行設置值0,現在您的代碼工作(請參閱我的示例代碼),或者您需要使用.text並檢查條件

if(strUser1=="---Select---") 
{ 
//alert 
}