2010-11-20 15 views
0

我有一個表單,其中包含一個名爲「wmi_cell_phone」的TextBox和一個RadioButtonList「wmi_send_sms」。 基本上,我需要創建一個驗證器來檢查所選收音機的值是否爲「Y」。如果是這種情況,那麼它檢查TextBox值是否爲空。如果它是空的,那麼它應該通知用戶輸入一個值。使用CustomValidator檢查asp.net中的RadioButtonList和TextBox值時遇到問題

這裏是我的.aspx代碼:

<asp:TextBox ID="wmi_cell_phone" runat="server" MaxLength="100" Width="200px"></asp:TextBox> 
<asp:RadioButtonList ID="wmi_send_sms" RepeatDirection="Horizontal" runat="server" Width="140px" CssClass="radio"></asp:RadioButtonList> 

和代碼隱藏(VB):

wmi_send_sms.Items(0).Value = "Y" 
wmi_send_sms.Items(1).Value = "N" 

我的驗證

<asp:CustomValidator ID="val_wmi_send_sms" runat="server" 
ClientValidationFunction="ValidateSMS" 
Display= "Dynamic" 
ErrorMessage="Please enter a valid phone number."> </asp:CustomValidator> 

<script language="javascript" type="text/javascript">     
function ValidateSMS(Source, args) 
{ 

    var smsRadio = document.getElementsByName('<%= wmi_send_sms.ClientID %>'); 
     var cellphone = document.getElementById('<%= wmi_cell_phone.ClientID %>');  

    for (var x = 0; x < smsRadio.length; x ++) 
    { 
     if (smsRadio[x].checked) 
     { 
      if (smsRadio[x].value == "Y") 
      { 
       if (cellphone.value == "") 
        args.IsValid = false; 
       else 
        args.IsValid = true; 
      } 
     } 
    } 
} 
</script> 

卜它似乎不工作..也許我以錯誤的方式訪問RadioButtonList ..

+0

什麼意思'它似乎沒有work'在你的驗證檢查? 你是否已經調試過js函數?發佈你的驗證器的標記。 – 2010-11-20 18:23:11

+0

我的意思是,它沒有做它應該做的事情......如果用戶選擇值爲「是」的收音機,並將文本框留空,它應該提醒用戶他應該在文本框中輸入一個值..但它什麼也不做 – Joe 2010-11-20 20:00:14

+0

您是否已經調試過if js函數被調用?使用ie開發工具:http://www.microsoft.com/downloads/en/details.aspx?FamilyID=95e06cbe-4940-4218-b75d-b8856fced535或FireBug for Firefox:https://addons.mozilla.org/ en-US/firefox/addon/1843/ – 2010-11-20 20:23:51

回答

1

您應該首先調試Validator是否調用ClientValidationFunction。

如果確認者應即使沒有被選中,你必須設置ValidateEmptyText爲true工作。

  • ValidationGroup值是否在所有3個位置匹配:提交按鈕,驗證器控件和要驗證的控件?
  • 提交按鈕是否具有CausesValidation = true?
  • RadioButtonList是否有CausesValidation = true?
0

「也許我訪問 RadioButtonList的錯誤的方式。」

沒錯。

看看生成的標記,這將是這樣的:

<table id="wmi_send_sms" class="radio" style="width:140px;"> 
    <tr> 
    <td> 
     <input id="wmi_send_sms_0" type="radio" name="wmi_send_sms" value="Y" /> 
     <label for="wmi_send_sms_0">Y</label> 
    </td> 
    <td> 
     <input id="wmi_send_sms_1" type="radio" name="wmi_send_sms" value="N" /> 
     <label for="wmi_send_sms_1">N</label> 
    </td> 
    </tr> 
</table> 

<%= wmi_send_sms.ClientID %>標籤越來越表封閉的單選按鈕列表的ID。

就個人而言,在客戶端工作時,我發現避免RadioButtonList要容易得多。只需創建兩個RadioButton控件,使用GroupName屬性將它們相互關聯。

<asp:RadioButton ID="wmi_send_sms_y" runat="server" GroupName="wmi_send_sms" Text="Y" /> 
<asp:RadioButton ID="wmi_send_sms_n" runat="server" GroupName="wmi_send_sms" Text="N" /> 

然後你可以檢查「Y」一個是通過訪問<%= wmi_send_sms_y.ClientID %>

+0

感謝Carson63000爲您提供的信息,非常感謝!不幸的是,我正在爲我工​​作的公司的客戶開展現有項目。我不能改變應用程序的設計(所以我不能從RadioButtonList更改爲RadioButton)..我允許做的就是將Validator添加到現有的代碼:(你知道它是怎麼回事! – Joe 2010-11-21 17:21:10

+0

好吧,用一些小竅門,我找不到任何超級簡單的方法來獲取RadioButtonList中單個按鈕的客戶端ID,但是<%= wmi_send_sms.ClientID%>會得到你可以使用的表格看看裏面 - 最終,你需要找到實際的元素並查看選中的內容,如果你可以使用它,jQuery會使得這更簡單 – Carson63000 2010-11-21 22:28:16

+0

或者,你*可以*只是假設第一個的ID將是RadioButtonList的ClientID,其末尾帶有「_0」;即getElementsByName('<%= wmi_send_sms.ClientID%> _ 0')但我不是100%確定這是將是安全的或保證永不改變。 – Carson63000 2010-11-21 22:30:01