如果在gridview中选中复选框,则验证下拉列表

时间:2013-02-07 09:26:08

标签: c# asp.net

<asp:GridView ID="gvAttributes" runat="server" 
              AutoGenerateColumns="false" EnableModelValidation="True" 
              CssClass="GridViewStyle" onrowdatabound="gvAttributes_RowDataBound">
  <Columns>
    <asp:TemplateField HeaderText="Select">
      <ItemTemplate>
        <asp:CheckBox ID="cbxAttributescheck" runat="server" Text=''
        <%# Eval("AttributeName") %> AutoPostBack="true" OnCheckedChanged="cbxAttributescheck_CheckedChanged"/>
        <asp:HiddenField ID="hdnAttributeValue" runat="server" Value=''
        <%# Eval("AttributeID") %>' />
        <asp:Label ID="lblDelete" runat="server" Text=''
          <%# Eval("AttributeID") %>'   Visible="false"></asp:Label>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Data Type" >
      <ItemTemplate>
        <asp:DropDownList ID="ddlAttributesDataType" runat="server">
        </asp:DropDownList>
        <asp:RequiredFieldValidator ID="rfvAttributeType" runat="server" 
                                    ErrorMessage="*"  Enabled="false" 
                                    ControlToValidate="ddlAttributesDataType" InitialValue="0">
        </asp:RequiredFieldValidator>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Allow Nulls" >
      <ItemTemplate>
        <asp:CheckBox ID="cbxAttributesisnull" runat="server" />
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

protected void cbxAttributescheck_CheckedChanged(object sender, EventArgs e)
{
    CheckBox cbx = (CheckBox)sender;
    string CurrentCbxId = ((CheckBox)sender).ClientID;
    foreach (GridViewRow Row in gvAttributes.Rows)
    {
        if (((CheckBox)Row.FindControl("cbxAttributescheck")).ClientID.Equals(CurrentCbxId) && cbx.Checked)
        {
            RequiredFieldValidator rfvAttributeType = (RequiredFieldValidator)Row.FindControl("rfvAttributeType");
            rfvAttributeType.Enabled = true;
        }
    }
}

我已经按照上面的网格视图我想要在选中复选框时启用下拉列表所需的字段验证器。我想不是通过JavaScript来做这个服务器端。我试过但无法解决问题。我在复选框选中时尝试启用验证器,否则将禁用验证器。我需要帮助。

1 个答案:

答案 0 :(得分:0)

刚刚修改了复选框已更改事件并将已分配的验证组复选到复选框和下拉列表。

protected void cbxAttributescheck_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox cbx = (CheckBox)sender;
        string CurrentCbxId = ((CheckBox)sender).ClientID;
        foreach (GridViewRow Row in gvAttributes.Rows)
        {
            RequiredFieldValidator rfvAttributeType = (RequiredFieldValidator)Row.FindControl("rfvAttributeType");
            if (((CheckBox)Row.FindControl("cbxAttributescheck")).ClientID.Equals(CurrentCbxId))
            {
                rfvAttributeType.Enabled = cbx.Checked;
            }            
        }
    }