Answered by:
Set causesvalidation in codebehind in c#

Question
-
User-2004582644 posted
Hi,
On the form I have hidden the TextBox tx2.
The TextBox tx2 becomes visible a certain condition to be completed.
I need to make the text field required according to a certain condition that occurs in the code behind.
I have tried this, but the forum is always validate.
Thank in advance for any help.
<asp:TextBox ID="tx2" runat="server" CausesValidation="false" ValidationGroup="Group1" Visible="false"></asp:TextBox> if (i > 0) { tx2.Text = string.Empty; tx2.Visible = true; tx2.CausesValidation = true; }
Friday, April 10, 2020 4:33 PM
Answers
-
User753101303 posted
Hi,
Seems you believe CausesValidation would make it required? See rather https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.requiredfieldvalidator?view=netframework-4.8
Another option is to use https://docs.microsoft.com/en-us/aspnet/web-forms/overview/presenting-and-managing-data/model-binding/ to bind the UI to an object and use https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/data-annotations#required
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, April 10, 2020 6:03 PM -
User-18289217 posted
I assume that you want to make the txt2 required depending on the decision made in the code-behind. If I am right, then please notice that CausesValidation has nothing to do with that. When CausesValidation is set to true it only indicates whether validation is performed not if certain control to be validated.
In your case you need to enable certain required field validator. I prepared a small demo here:
<div class="container"> <h1 class="page-header">Dynamic Validation Example</h1> <div class="row"> <div class="col-xs-12"> <div class="form-group"> <asp:Label AssociatedControlID="txtName" runat="server" Text="Your name:" /> <asp:TextBox ID="txtName" runat="server" TextMode="SingleLine" CssClass="form-control" /> <asp:RequiredFieldValidator runat="server" ID="reqName" ControlToValidate="txtName" ValidationGroup="Group1" ErrorMessage="Name is required!" Enabled="true" ForeColor="Red" Display="Dynamic" /> </div> </div> </div> <div class="row"> <div class="col-xs-12"> <div class="form-group"> <asp:Label AssociatedControlID="rblAge" runat="server" Text="Are you over 18 years old?" /> <asp:RadioButtonList runat="server" ID="rblAge" RepeatDirection="Horizontal" OnSelectedIndexChanged="rblAge_SelectedIndexChanged" AutoPostBack="true"> <asp:ListItem Text="Yes" Value="1"></asp:ListItem> <asp:ListItem Text="No" Value="0"></asp:ListItem> </asp:RadioButtonList> </div> </div> </div> <div class="row"> <div class="col-xs-12"> <div class="form-group"> <asp:Label AssociatedControlID="txtEmail" runat="server" Text="Your e-mail:" /> <asp:TextBox ID="txtEmail" runat="server" TextMode="Email" CssClass="form-control" /> <asp:RequiredFieldValidator runat="server" ID="reqEmail" ControlToValidate="txtEmail" ValidationGroup="Group1" ErrorMessage="E-mail is required!" Enabled="false" ForeColor="Red" Display="Dynamic" /> </div> </div> </div> <div class="row"> <div class="col-xs-12"> <div class="form-group"> <asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="btn btn-primary" OnClick="btnSubmit_Click" ValidationGroup="Group1" /> </div> </div> </div> </div>
code-behind:
protected void rblAge_SelectedIndexChanged(object sender, EventArgs e) { if (rblAge.SelectedValue == "1") reqEmail.Enabled = true; else reqEmail.Enabled = false; }
HTH
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, April 12, 2020 8:22 AM
All replies
-
User-943250815 posted
Take a look at Custom Validator
https://asp.net-tutorials.com/validation/custom-validator/Friday, April 10, 2020 5:01 PM -
User-2004582644 posted
???
Is that right for me? I don't believe
Friday, April 10, 2020 5:32 PM -
User753101303 posted
Hi,
Seems you believe CausesValidation would make it required? See rather https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.requiredfieldvalidator?view=netframework-4.8
Another option is to use https://docs.microsoft.com/en-us/aspnet/web-forms/overview/presenting-and-managing-data/model-binding/ to bind the UI to an object and use https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/data-annotations#required
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, April 10, 2020 6:03 PM -
User-18289217 posted
I assume that you want to make the txt2 required depending on the decision made in the code-behind. If I am right, then please notice that CausesValidation has nothing to do with that. When CausesValidation is set to true it only indicates whether validation is performed not if certain control to be validated.
In your case you need to enable certain required field validator. I prepared a small demo here:
<div class="container"> <h1 class="page-header">Dynamic Validation Example</h1> <div class="row"> <div class="col-xs-12"> <div class="form-group"> <asp:Label AssociatedControlID="txtName" runat="server" Text="Your name:" /> <asp:TextBox ID="txtName" runat="server" TextMode="SingleLine" CssClass="form-control" /> <asp:RequiredFieldValidator runat="server" ID="reqName" ControlToValidate="txtName" ValidationGroup="Group1" ErrorMessage="Name is required!" Enabled="true" ForeColor="Red" Display="Dynamic" /> </div> </div> </div> <div class="row"> <div class="col-xs-12"> <div class="form-group"> <asp:Label AssociatedControlID="rblAge" runat="server" Text="Are you over 18 years old?" /> <asp:RadioButtonList runat="server" ID="rblAge" RepeatDirection="Horizontal" OnSelectedIndexChanged="rblAge_SelectedIndexChanged" AutoPostBack="true"> <asp:ListItem Text="Yes" Value="1"></asp:ListItem> <asp:ListItem Text="No" Value="0"></asp:ListItem> </asp:RadioButtonList> </div> </div> </div> <div class="row"> <div class="col-xs-12"> <div class="form-group"> <asp:Label AssociatedControlID="txtEmail" runat="server" Text="Your e-mail:" /> <asp:TextBox ID="txtEmail" runat="server" TextMode="Email" CssClass="form-control" /> <asp:RequiredFieldValidator runat="server" ID="reqEmail" ControlToValidate="txtEmail" ValidationGroup="Group1" ErrorMessage="E-mail is required!" Enabled="false" ForeColor="Red" Display="Dynamic" /> </div> </div> </div> <div class="row"> <div class="col-xs-12"> <div class="form-group"> <asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="btn btn-primary" OnClick="btnSubmit_Click" ValidationGroup="Group1" /> </div> </div> </div> </div>
code-behind:
protected void rblAge_SelectedIndexChanged(object sender, EventArgs e) { if (rblAge.SelectedValue == "1") reqEmail.Enabled = true; else reqEmail.Enabled = false; }
HTH
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, April 12, 2020 8:22 AM