Disable RequiredFieldValidator in asp.net c# - c#

I am trying to disable a RequiredFieldValidator by radio button list value. I have it coded but it doesn't seems to be working.
What i have done so far:
protected void radioButtonList(object sender, EventArgs e)
{
if (((RadioButtonList)dvInsertPromotion.FindControl("RadioButtonList2")).SelectedValue == "Y")
{
((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate1")).Enabled = false;
((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate")).Enabled = false;
this.addPromotion.Show();
}
else
{
((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate1")).Enabled = true;
((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate")).Enabled = true;
this.addPromotion.Show();
}
}
html:
<asp:RadioButtonList ID="RadioButtonList2" runat="server" ValidationGroup="addValidationGp" OnSelectedIndexChanged="radioButtonList">
<asp:ListItem Text="Yes" Value="Y"></asp:ListItem>
<asp:ListItem Text="No" Value="N" Selected></asp:ListItem>
</asp:RadioButtonList>
<asp:TextBox ID="txtPubDate" Width="75" MaxLength="10" runat="server" AutoPostBack="true" OnTextChanged="insertStartEndDateTime_SelectedIndexChanged"/>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtPubDate"
PopupPosition="Right" Format="dd/MM/yyyy" />
<asp:RequiredFieldValidator ID="rfvdate" ValidationGroup="addValidationGp" runat="server"
ControlToValidate="txtPubDate" ErrorMessage="*" Display="Dynamic" Enabled="true"
SetFocusOnError="true" /><br />

I've had the same problem in the past and the only way I was able to solve this was by making the validators invisible altogether:
((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate1")).Visible = false;
Just don't forget to set them to visible again whenever you want to enable them.

Related

If condition for required field validation

I have a DropDownList and a TextBox in c#.net. If DropDownList value is "No" then some value must enter in TextBox. If dropdownlist value is yes then required field validator is not needed for that TextBox. How to make it possible?
<asp:DropDownList ID="dropdownlist1" runat="server"
CssClass="NormalText" Width="155px" AutoPostBack="true"
onselectedindexchanged="ddls_SelectedIndexChanged">
<asp:ListItem Selected="True" Value=""></asp:ListItem>
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:TextBox ID="Textbox1" runat="server" CausesValidation="True"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="Textbox1" ErrorMessage="Explanation needed If you select NO">
</asp:RequiredFieldValidator>
</td>
protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
}
Use this code for conditional validation:
protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
if(dropdownlist1.SelectedValue == "No")
{
RequiredFieldValidator1.Enabled = true;
}
else if(dropdownlist1.SelectedValue == "Yes")
{
RequiredFieldValidator1.Enabled = false;
}
}
Optionally, you may also have to set Submit Button Validation group:
ButtonSubmit.ValidationGroup = string.Empty;
when field validator is not enabled.

Require TextBox depending on RadioButtonList selection in Repeater

I have a repeater with a RadioButtonList and TextBox inside. I want the TextBox to be required only if the RadioButtonList has a value of 1 or 2. How can I achieve this?
I thought I could initially enable the RFV on a selected index change but it can't see the items since it is within a repeater. Do I need to do this within the ItemDataBound?
<asp:Repeater ID="repeaterSurvey" runat="server">
<ItemTemplate>
<div class="form-group">
<asp:Label ID="labelQuestion" runat="server" Text='<%# Eval("Question")%>' />
<asp:RequiredFieldValidator ID="RFV1" CssClass="required" runat="server"
ErrorMessage="Required" Display="Dynamic" ControlToValidate="surveyList" />
<asp:RadioButtonList RepeatDirection="Horizontal" ID="surveyList" AutoPostBack="true"
OnSelectedIndexChanged="surveyList_SelectedIndexChanged" runat="server">
<asp:ListItem Value="1">Strongly Disagree</asp:ListItem>
<asp:ListItem Value="2">Disagree</asp:ListItem>
<asp:ListItem Value="3">Agree</asp:ListItem>
<asp:ListItem Value="4">Strongly Agree</asp:ListItem>
<asp:ListItem Value="0">N/A</asp:ListItem>
</asp:RadioButtonList>
<asp:HiddenField ID="hiddenfieldID" Value='<%# Eval("ID")%>' runat="server" />
<asp:TextBox ID="textboxComment" Placeholder="Comments" CssClass="form-control" TextMode="MultiLine"
Rows="3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RFV2" Enabled="false" CssClass="required" runat="server"
ErrorMessage="Comment Required" Display="Dynamic" ControlToValidate="textboxComment" />
</div>
</ItemTemplate>
</asp:Repeater>
Codebehind
protected void surveyList_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (surveyList.SelectedValue == "1" || surveyList.SelectedValue == "2")
{
RFV2.Enabled = true;
}
else
{
RFV2.Enabled = false;
}
}
With the help of the comment section (stephen.vakil) I was able to create a solution to my problem:
protected void surveyList_SelectedIndexChanged(object sender, System.EventArgs e)
{
RadioButtonList surveyList = (RadioButtonList)sender;
string value = surveyList.SelectedValue;
RepeaterItem row = (RepeaterItem)surveyList.NamingContainer;
if (value == "1" || value == "2")
{
((RequiredFieldValidator)row.FindControl("RFV2")).Enabled = true;
}
else
{
((RequiredFieldValidator)row.FindControl("RFV2")).Enabled = false;
}
}

Why asp.net button is not hiding?

I am facing a weird problem in asp.net forms. I am trying to make button invisible/Inactive but none of my code works in any situation. It remains visible/active.
<asp:Button ID="btnPrintEditedSms" ValidationGroup="Complaints" runat="server" CssClass="btn btn-success"
OnClick="btnPrintEditedSms_Click" Text="Send" />
I am trying to put code here, to make it visible or inactive but not doesn't work although other statements work
protected void GridViewAllSms_SelectedIndexChanged(object sender, EventArgs e)
{
BtnPrintEditedSms.Visible = false; //this doesn't work
BtnPrintEditedSms.Enabled = false; //this also
txtComplainant.Visible = true; //this works
}
It is within Update Panel:
<asp:UpdatePanel ID="updGridViewSMS" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<label><b>Search By Date Range</b></label>
<asp:Label ID="lblDateFrom" runat="server" Text="From"></asp:Label>
<asp:TextBox ID="txtFromDate" runat="server" ></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtenderFromDate" Format="dd/MMM/yyyy" TargetControlID="txtFromDate" runat="server">
</asp:CalendarExtender>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server"
ControlToValidate="txtFromDate" Display="None" ErrorMessage=""
ForeColor="Red" >
</asp:RequiredFieldValidator>
<asp:Label ID="lblDateTo" runat="server" Text="To"></asp:Label>
<asp:TextBox ID="txtToDate" runat="server"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtenderToDate" Format="dd/MMM/yyyy" TargetControlID="txtToDate" runat="server">
</asp:CalendarExtender>
<asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server"
ControlToValidate="txtToDate" Display="None" ErrorMessage=""
ForeColor="Red" >
</asp:RequiredFieldValidator>
<asp:Button ID="btnSearchByDate" CssClass="btn btn-success" runat="server" Text="Search"
ClientIDMode="Static" OnClick="btnSearchByDate_Click" />
<asp:Button ID="btnEdit" CssClass="btn btn-success" runat="server" Text="Edit"
ClientIDMode="Static" OnClick="btnEdit_Click" />
</asp:UpdatePanel>
try this
protected void GridViewAllSms_SelectedIndexChanged(object sender, EventArgs e)
{
BtnPrintEditedSms.Visible = false; //this doesn't work
BtnPrintEditedSms.Enabled = false; //this also
txtComplainant.Visible = true; //this works
Updatepanel1.Update();
}
or if you dont want to set your update mode set to conditional then set it to always like
<asp:UpdatePanel ID="Updatepanel1" runat="server" UpdateMode="Always">

Validate() function doesn't fire

Here is my button.
<asp:Button ID="btnNext" runat="server" Text="Next" Style="display: none" OnClick="btnNext_Click" CausesValidation="true" ValidationGroup="vgLinR"/>
When I write ValidationGroup="vgLinR" in aspx side validation works. But I have 2 different validation group. So I need fire these 2 validation group in one button.
so I write that code at code behind :
protected void btnNext_Click(object sender, EventArgs e)
{
Page.Validate("vgLinR");
Page.Validate("vgLogR");
}
but it doesn't work. Why? How can I do that?
it will work for you ..
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="*" Font-Size="Medium" ForeColor="Red" ValidationGroup="group 1">*</asp:RequiredFieldValidator>
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2" ErrorMessage="*" Font-Size="Medium" ForeColor="Red" ValidationGroup="group 1">*</asp:RequiredFieldValidator>
<br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="TextBox3" ErrorMessage="*" Font-Size="Medium" ForeColor="Red" ValidationGroup="group 2">*</asp:RequiredFieldValidator>
<br />
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="TextBox4" ErrorMessage="*" Font-Size="Medium" ForeColor="Red" ValidationGroup="group 2">*</asp:RequiredFieldValidator>
<br />
<br />
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="validate all" OnClick="Button1_Click"/> <br />
</div>
</form>
and write code for onclick event
protected void Button1_Click(object sender, EventArgs e)
{
Page.Validate();
}
try this
public bool Validate()
{
var isValid = false;
Page.Validate("vgLinR");
isValid = Page.IsValid;
if (isValid)
{
Page.Validate("vgLogR");
isValid = Page.IsValid;
}
return isValid;
}
Saw your answer reply for Amit a bit too late. I have updated my answer accordingly. May be you can use a similar idea if it does not fit your requirement.
In my code I am using a single ValidationSummary control without any validation group specified. Also remove the validation group from your button. Textbox a and b can be in one validation group, vg1 whereas Textbox c and d can be in another, vg2. I am not sure how you have set up your validation groups.
protected void btnNext_Click(object sender, EventArgs e)
{
if (RadioButton1.Checked)
{
Page.Validate("vg1");
ValidationSummary1.ValidationGroup = "vg1";
}
else if (RadioButton2.Checked)
{
Page.Validate("vg2");
ValidationSummary1.ValidationGroup = "vg2";
}
if (Page.IsValid)
{
//do something in here
}
}
The above code will do a server side validation. To do it on the client side as well, you would need to add a bit of javascript.
Look at another post to enable/disable Validation Group from JQuery or Javascript

customvalidator onServerValidate not firing

I have a Radio button List and Text Box both with validation.
<asp:RadioButtonList ID="member" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
<asp:requiredfieldvalidator id="unionvalidator" runat="server" controltovalidate="member" errormessage="Required" />
Required if member == "yes"
<asp:TextBox runat="server" ID="union"></asp:TextBox>
<asp:customvalidator ID="Customvalidator1" runat="server" ValidateEmptyText="true" onServerValidate="UnionValidate" errormessage="Your current union is required" />
My ServerValidate which doesn't fire at all.
public void UnionValidate(object source, ServerValidateEventArgs args)
{
if (member.Text == "yes" && union.Text.Trim() == "")
args.IsValid = false;
}
Are you calling the Page.Validate() method somewhere in your code behind or does the submit button has the CausesValidation set to true?

Categories