Tuesday, July 16, 2013

Calling Required field validator from Code behind

//.aspx page

<asp:dropdownlist appenddatabounditems="true" autopostback="True" datatextfield="ChannelNo" datavaluefield="Id" id="ddlChannel" onselectedindexchanged="ddlChannel_SelectedIndexChanged" runat="server">
 </asp:dropdownlist>
  &nbsp;<asp:requiredfieldvalidator controltovalidate="ddlChannel" cssclass="ErrorMessage" errormessage="Select Channel Name" forecolor="" id="RequiredFieldValidator1" initialvalue="--Select--" runat="server" setfocusonerror="true" validationgroup="GnReport">
</asp:requiredfieldvalidator>

//.cs page

call it inside button click

   if (ddlChannel.Text == "0")
 
{

  RequiredFieldValidator1.Validate();
   
 RequiredFieldValidator1.IsValid = false;
 
 RequiredFieldValidator1.Visible = true;
 
  RequiredFieldValidator1.Enabled = true;
 
  RequiredFieldValidator1.ErrorMessage = "Select Channel Name.";
 
  return;
                }

No comments:

Post a Comment