2012-05-12 158 views
3

下面是我的頁面的輸出運行下面的代碼後:如何控制ASP.NET驗證程序控件客戶端驗證

我用在我的處境ValidatorEnable客戶端驗證(如果你知道如何更好的方法做到這一點,讓我知道)

我的問題是:

IHAVE使用複選框一個GridView和一個下拉列表彼此相鄰如圖所示的屏幕截圖。

因此,如果用戶選中了複選框,它將啓用下拉列表以供選擇。 如果用戶點擊提交按鈕,它會驗證並要求用戶從下拉列表中選擇任何選項。

問題:

但問題是:它驗證了所有行,而不是一個我點擊。

我該如何強制驗證用戶在複選框中選中的特定行?

enter image description here

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" OnRowDataBound="gv_RowDataBound" 
      EnableModelValidation="True"> 
      <Columns> 
       <asp:BoundField DataField="ID" ControlStyle-Width="250px" HeaderText="ID" SortExpression="ID" /> 
       <asp:BoundField DataField="FirstName" ControlStyle-Width="250px" HeaderText="FirstName" 
        SortExpression="FirstName" /> 
       <asp:BoundField DataField="LastName" ControlStyle-Width="250px" HeaderText="LastName" 
        SortExpression="LastName" /> 
       <asp:TemplateField> 
        <ItemTemplate> 
         <asp:CheckBox ID="checkbox1" runat="server" /> 
         <asp:DropDownList ID="ddl_PaymentMethod" runat="server"> 
            <asp:ListItem Value="-1">----</asp:ListItem> 
            <asp:ListItem Value="0">Month</asp:ListItem> 
            <asp:ListItem Value="1">At End</asp:ListItem> 
            <asp:ListItem Value="2">At Travel</asp:ListItem> 
           </asp:DropDownList> 
         <asp:RequiredFieldValidator ID="requiredDDL" runat="server" ControlToValidate="ddl_PaymentMethod" 
          ErrorMessage="Please select" InitialValue="-1" Display="Dynamic"></asp:RequiredFieldValidator> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="Value"> 
        <ItemTemplate> 
         <asp:TextBox ID="txt_Value" runat="server" Width="58px" Text="0"></asp:TextBox> 
        </ItemTemplate> 
       </asp:TemplateField> 
      </Columns> 
     </asp:GridView> 


protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
if (e.Row.RowType == DataControlRowType.DataRow) 
{ 
    CheckBox checkbox1 = (CheckBox)e.Row.FindControl("checkbox1"); 
    DropDownList ddl_PaymentMethod = (DropDownList)e.Row.FindControl("ddl_PaymentMethod"); 
    ddl_PaymentMethod.Attributes.Add("disabled", "disabled"); 

    checkbox1.Attributes.Add("onclick", "javascript:EnableCheckBox('" + ddl_PaymentMethod.ClientID + "','" + checkbox1.ClientID + "')"); 
} 
} 

function EnableCheckBox(ddl, chk) { 

    var ddl_PaymentMethod = document.getElementById(ddl); 
    var _chkbox = document.getElementById(chk); 
    if (_chkbox.checked) { 
     ddl_PaymentMethod.disabled = false; 
     validateCheckBox(ddl_PaymentMethod, true); 
    } 
    else { 
     ddl_PaymentMethod.disabled = true; 
     validateCheckBox(ddl_PaymentMethod, false); 
    } 
} 

function validateCheckBox(ddl, state) { 
    ValidatorEnable(document.getElementById(ddl.id, state));    
} 
+0

請看到這個帖子非常類似的問題希望它能幫助 http://stackoverflow.com/questions/10432090/conditional-requiredfieldvalidator-inside-of-a-repeater/10432932#10432932 –

+0

請看到這個帖子非常類似的問題希望幫助 http://stackoverflow.com/questions/10432090/conditional-requiredfieldvalidator-inside-of-a-repeater/10432932#10432932 –

+0

感謝您的鏈接,但代碼不完整,仍然coulnd不abel想通了 –

回答

3

得到它爲你工作... GridView控件:

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" OnRowDataBound="gv_RowDataBound"> 
     <Columns> 
      <asp:BoundField DataField="ID" ControlStyle-Width="250px" HeaderText="ID" SortExpression="ID" /> 
      <asp:BoundField DataField="FirstName" ControlStyle-Width="250px" HeaderText="FirstName" 
       SortExpression="FirstName" /> 
      <asp:BoundField DataField="LastName" ControlStyle-Width="250px" HeaderText="LastName" 
       SortExpression="LastName" /> 
      <asp:TemplateField> 
       <ItemTemplate> 
        <asp:CheckBox ID="checkbox1" runat="server" /> 
        <asp:DropDownList ID="drpPaymentMethod" runat="server"> 
           <asp:ListItem Value="-1">----</asp:ListItem> 
           <asp:ListItem Value="0">Month</asp:ListItem> 
           <asp:ListItem Value="1">At End</asp:ListItem> 
           <asp:ListItem Value="2">At Travel</asp:ListItem> 
          </asp:DropDownList> 
        <asp:RequiredFieldValidator ID="rfv" InitialValue="-1" ControlToValidate="drpPaymentMethod" Enabled="false" Display="Static" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator> 

       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Value"> 
       <ItemTemplate> 
        <asp:TextBox ID="txt_Value" runat="server" Width="58px" Text="0"></asp:TextBox> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 

CS:

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      CheckBox checkbox1 = e.Row.FindControl("checkbox1") as CheckBox; 
      RequiredFieldValidator rfv = e.Row.FindControl("rfv") as RequiredFieldValidator; 
      DropDownList drpPaymentMethod = (DropDownList)e.Row.FindControl("drpPaymentMethod"); 
      // you can just pass "this" instead of "myDiv.ClientID" and get the ID from the DOM element 
      checkbox1.Attributes.Add("onclick", "UpdateValidator('" + checkbox1.ClientID + "','" + drpPaymentMethod.ClientID + "','" + rfv.ClientID + "');"); 
      if (!checkbox1.Checked) 
       drpPaymentMethod.Attributes.Add("disabled", "disabled"); 
     } 
    } 

的javascript:

function UpdateValidator(chkID, drpID, validatorid) { 
     //enabling the validator only if the checkbox is checked 
     var enableValidator = $("#" + chkID).is(":checked"); 

     if (enableValidator) 
      $('#' + drpID).removeAttr('disabled'); 
     else 
      $('#' + drpID).attr('disabled', 'disabled'); 

     var vv = $('#' + validatorid).val(); 

     ValidatorEnable(document.getElementById(validatorid), enableValidator); 
    } 
+0

讓我試試謝謝你... 1+ –

+0

我有類似的問題,並張貼在這裏,請看看http://stackoverflow.com/questions/10662245/checkbox-in-headertemplate-validates-itemtemplate-checkbox –

+1

我發佈一個答案,一切順利! –