Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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;
        }
    }
}
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 6-Feb-13 23:44pm
v2
Comments
Avik Ghosh22 at 7-Feb-13 4:57am
   
<asp:DropDownList ID="ddlAttributesDataType" runat="server" required>

 
"required" is required field validator in HTML5 .... try this...
Rajasekhar_Raj at 7-Feb-13 5:08am
   
Not in Html5 We need in Asp.net
Avik Ghosh22 at 7-Feb-13 5:12am
   
ok.
Tadit Dash at 7-Feb-13 6:36am
   
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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100