Click here to Skip to main content
15,909,324 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<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>

C#
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;
        }
    }
}

I took grid view as above.
I want to enable the required field validator for drop down, when check box is checked.
I want to do this server side not through JavaScript.
I tried but unable to work out. I tried to enable the validator, when check box checked else it will be disabled.
I need assistance on this.
Posted
v2
Comments
Avik Ghosh22 7-Feb-13 4:57am    
<asp:DropDownList ID="ddlAttributesDataType" runat="server" required>


"required" is required field validator in HTML5 .... try this...
Rajasekhar_Raj 7-Feb-13 5:08am    
Not in Html5 We need in Asp.net
Avik Ghosh22 7-Feb-13 5:12am    
ok.
Why not to do it in jQuery?
That will be easier and will eliminate extra burden for server as it is calling server code for check change.

1 solution

a validator needs ValidationGroup property to have some value to work, assign a Validation Group (say 'validate') to check box, drop down and the button on which you want to fire validation. Give it a try and post your outcome.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900