2012-05-19 91 views
2

問:複選框在HeaderTemplate中驗證的ItemTemplate複選框

我想,如果用戶選中該複選框,你可以在屏幕上看到一個驗證下拉列表。

問題:

那麼,如何可以驗證該複選框,如果用戶有選擇所有從標題中的複選框?因爲你可以看到兩個屏幕。如果我選擇所有複選框,那麼我正在尋找啓動驗證,如屏幕一所示。

輸出:

屏幕1: enter image description here

屏幕2

enter image description here

// ASPX

<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 HeaderText="Reject"> 
     <HeaderTemplate > 
      Reject<br /> 
      <asp:CheckBox ID="checkboxall" runat="server" /> 

       <asp:DropDownList ID="drpPaymentMethod_header" runat="server"> 
        <asp:ListItem Value="-1">Please select reason</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> 

     </HeaderTemplate> 
      <ItemTemplate> 
       <asp:CheckBox ID="checkbox1" CssClass="selectreject" runat="server" /> 
       <asp:DropDownList ID="drpPaymentMethod" runat="server"> 
        <asp:ListItem Value="-1">Please select reason</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" ForeColor="Red" SetFocusOnError="true" 
        Enabled="false" Display="dynamic" runat="server" ErrorMessage="Please select reason"></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"); 
      } 
      if (e.Row.RowType == DataControlRowType.Header) 
      { 
       CheckBox checkboxall = e.Row.FindControl("checkboxall") as CheckBox; 
       //how to get the reference here (rfv ?????????) 
       //checkboxall.Attributes.Add("onclick", "UpdateValidatorAll('" + checkboxall.ClientID + "','" + drpPaymentMethod.ClientID + "','" + rfv.ClientID + "');"); 

      } 
     } 

// JS

$(document).ready(function() { 

      var checkbox1 = "#<%=gv.ClientID%> input[id*='checkbox1']:checkbox"; 
      var checkboxall = $("input[id$='checkboxall']"); 

      $(checkboxall).click(function() { 
       if (checkboxall.is(':checked')) { 
        $('.selectreject > input').attr("checked", checkboxall.attr("checked")); 
       } 
       else { 
        $('.selectreject > input').attr("checked", false); 
       } 

      }); 

     }); 

     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); 
     } 

     function UpdateValidatorAll(....) // i havent write the code for this function (select All checkbox) 

回答

3

試試這個: ASPX:

<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> 
      <HeaderTemplate> 
       Reject<br /> 
       <asp:CheckBox ID="checkboxall" runat="server" /> 
       <asp:DropDownList ID="drpPaymentMethod_header" runat="server"> 
        <asp:ListItem Value="-1">Please select reason</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> 
      </HeaderTemplate> 
      <ItemTemplate> 
       <asp:CheckBox ID="checkbox1" runat="server" /> 
       <asp:DropDownList ID="drpPaymentMethod" CssClass="gridDropDown" 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" CssClass="gridRfv" 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> 
<asp:Button ID="Button1" runat="server" Text="Button" /> 

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"); 
     } 

     if (e.Row.RowType == DataControlRowType.Header) 
     { 
      CheckBox checkboxall = e.Row.FindControl("checkboxall") as CheckBox; 

      checkboxall.Attributes.Add("onclick", "UpdateValidatorAll('" + checkboxall.ClientID + "');"); 

     } 
    } 

的JavaScript:

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> 
<script type="text/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); 
    } 

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

     $('.gridDropDown').each(function (index) { 
      if (enableValidator) 
       $(this).removeAttr('disabled'); 
      else 
       $(this).attr('disabled', 'disabled'); 
     }); 

     $('.gridRfv').each(function (index) { 
      var cont = $(this).get(); 
      ValidatorEnable(cont[0], enableValidator); 

     }); 

    } 
</script> 
+0

感謝1+ ..現在我得到的想法:) –