problem with asp.net validationcontrol - c#

I have a textbox and I have set required validationcontrol on it on click of a button.
<asp:TextBox runat="server" ID="name" Width="120"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvname" runat="server" ControlToValidate="name" ErrorMessage="Name is required" Display="Dynamic" />
</td>
but the problem is when I am clicking on modify shared webpart and when I click on APPLY or OK button it is not saving as my form is empty . and I can't put
CausesValidation="false" on that button as this button is by default in sharepoint.
Any idea how to resolve this...?

You can use the ValidationGroup property to group all the Validators and validation fields together.

Yes using the ValidationGroup tag will help to relate the controls in a single form and will tell the submit button which fields are associated. Also make sure you have a ValidationSummary control on the page with the same ValidationGroup value. Also consider adding an InitialValue="" onto the TextBox control.
<asp:ValidationSummary ID="vs1" ShowMessageBox="true" ShowSummary="false" DisplayMode="List" runat="server" ValidationGroup="Form" HeaderText="There were problems with your submission:" />
<asp:TextBox runat="server" ID="name" Width="120"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvname" ValidationGroup="Form" runat="server" ControlToValidate="name" InitialValue="" ErrorMessage="Name is required" Display="Dynamic" />
<asp:LinkButton ID="lnkSubmit" Text="Submit" OnClick="lnkSubmit_Click" ValidationGroup="Form" runat="server"></asp:LinkButton>

Related

HTML Required field not working as needed

In am using htm required="required" validation but it is not working properly as needed.
I have 2 button on single page and I want that on 1 button click it validate for textbox1 and on click of 2nd button it will validate for textbox2.
Please review attached image for more detail
You are looking for ValidationGroup. By giving a collection of Validators and a Button the same id, the corresponding button will only validate those fields.
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1" ValidationGroup="group1"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox2" ValidationGroup="group1"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button Group 1" ValidationGroup="group1" />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox3" ValidationGroup="group2"></asp:RequiredFieldValidator>
<asp:Button ID="Button2" runat="server" Text="Button Group 2" ValidationGroup="group2" />
I don't think HTML does that natively (grouped validation).
If server-side is not an option, then you'll need to use some JavaScript.
I have found some inspiration in this answer for a related question (Triggering HTML5 Form Validation) : https://stackoverflow.com/a/39689115/370786
If you group each set of inputs that you want to validate together in a form (so you will have several forms), then you can trigger validation through Javascript. Then you can stop the form from posting. This can be done through Javascript - maybe it can be done in HTML but I'm not sure.
Then once everything has validated you will need to post the data to the server, using some Javascript. It might be as simple as just triggering the submit() method on a form element.

Show ValidationErrors in Validation Summary only in asp.net

<asp:ValidationSummary ID="vsArea" runat="server" />
<div class="controls">
<asp:DropDownList ID="ddlDegreeLevel" runat="server"
placeholder="DegreeLevel" CssClass="form-control">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvDegreeLevel" runat="server"
ControlToValidate="ddlDegreeLevel"
Display="None" ForeColor="Red" InitialValue="-1"
ErrorMessage="Please select degree level" Text="">
</asp:RequiredFieldValidator>
</div>
Errormessage of "rfvDegreeLevel" is show on validation-Summary and also below the control. I also set Display="None" but still show on both area .
I want to show message only on Validation summary.
This is by design...message will be shown in validation control and in the summary. Basically you have 2 options
Show separate messages in your control and validation summary. E.g. you can put a red * in the validation control and detailed error message in the summary, which is a best practice. To achieve this, set both Text and ErrorMessage property of the validation control. Text will be appearing in the control and ErrorMessage will be appearing in the summary.
hide the message from control, set the Display property of the control to none..
You have to add validationgroup to all the controls. For this you can use the following code:
<asp:ValidationSummary ID="vsArea" runat="server" ValidationGroup="degree" />
<div class="controls">
<asp:DropDownList ID="ddlDegreeLevel" runat="server"
placeholder="DegreeLevel" CssClass="form-control">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvDegreeLevel" runat="server"
ValidationGroup="degree"
ControlToValidate="ddlDegreeLevel"
Display="None" ForeColor="Red" InitialValue="-1"
ErrorMessage="Please select degree level" Text="">
</asp:RequiredFieldValidator>
</div>

Submit by pressing enter asp.net

I am wondering why this, submitting on pressing enter, is only working on ie and not on google chrome...
This is the code I use actually :
<div class="TxtBox">
<asp:Panel ID="lepanel" runat="server" DefaultButton="Connect">
<asp:TextBox ID="TxtUserLogin" runat="server" TabIndex="1" Text="login" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
Display="Static" ControlToValidate="TxtUserLogin"></asp:RequiredFieldValidator>
<asp:TextBox ID="UserPass" runat="server" TabIndex="2" Text="password" TextMode="Password" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
Display="Static" ControlToValidate="UserPass"></asp:RequiredFieldValidator>
<asp:LinkButton ID="Connect" runat="server" OnClick="Connect_Click" TabIndex="3">connect</asp:LinkButton>
</asp:Panel>
<asp:Label ID="MsgError" runat="server" />
<div class="ForgottenPass">
forgotten password ?
</div>
<div class="RememberMe">
<asp:CheckBox runat="server" ID="chkBoxRemember" />
stay signed in
</div>
</div>
A LinkButton renders as a HTML anchor tag.
HTML anchor tags do not submit HTML forms. Therefore when you click Enter, this is not actioning any submit button.
A Button renders as HTML <input type="submit" />
An ImageButton renders as HTML <input type="image" />
These elements will both action your form.
Therefore changing LinkButton to Button or ImageButton is the best solution.
Using a LinkButton is also bad for users who do not have javascript enabled.
This is a known issue, when you add as default a link button (actually a link) and not a real button then there is a case that is not trigger properly.
So if you change it to real button you make it work.

Asp.net validators validate Disabled control

I have a Panel control that contains some control such as text boxes.I want to use asp.net validators to validate text boxes.But if Panle is disabled then text boxes become disabled but validators such as RequiredFieldValidator validate disabled text box.
<asp:Panel ID="Panel1" runat="server" Enabled="false">
<asp:TextBox ID="TextBox2" runat="server" />
<asp:RequiredFieldValidator runat="server" ErrorMessage="RequiredFieldValidator"
ForeColor="#FF3300" ControlToValidate="TextBox2"></asp:RequiredFieldValidator>
</asp:Panel>
How I can set for validators that don't validate disabled controls?
if some control is disabled you can set its property CausesValidation="False"
<asp:Button id="Button1" runat="server"
Text="Cancel" CausesValidation="False">
</asp:Button>
EDITED
can you do this way
if (!panel.Enabled)
{
RequiredFieldValidator1.Enabled = false;// disable your all validators
}
Add this line in Page_Load()
RequiredFieldValidator1.Enabled = panel.Enabled;

ASP.NET validation summary inside formview doesn't work

I have a validation summary inside a asp.net formview and the validators just don't seem to trigger the validation summary.
Things I already tried:
Enable the ViewState on everything.
Set the ClientID to static.
Set the ValidationGroup to the same group on both all validators and the summary.
Used Google to search for a solution.
Anyone has any ideas on how to handle this?
Code. Formview declaration:
<asp:FormView ID="FormViewPerson" runat="server" DataSourceID="ObjectDataSourcePerson"
DefaultMode="Edit" OnItemUpdating="FormViewPerson_ItemUpdating" OnItemCommand="FormViewPerson_ItemCommand"
OnItemUpdated="FormViewPerson_ItemUpdated" Width="100%">
Any of the textboxes with the validator:
<td> <asp:TextBox ID="NumberTextBox" runat="server" Text='<%# Bind("Number") %>'
TabIndex="10" CausesValidation="True" ClientIDMode="Static" />
<asp:RequiredFieldValidator ID="RequiredFieldValidatorNumber" runat="server" ControlToValidate="NumberTextBox"
ErrorMessage="Number is Required" ForeColor="Red"
ValidationGroup="EditPerson">*</asp:RequiredFieldValidator>
</td>
And the submitbutton:
<asp:ValidationSummary ID="ValidationSummaryPerson" runat="server"
ForeColor="Red" ClientIDMode="Static" CssClass="validation"
ShowMessageBox="True" ValidationGroup="EditPerson" ViewStateMode="Enabled"
/>
<asp:Button ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="Save" ValidationGroup="EditPerson" CssClass="ButtonStyle" TabIndex="90" />
<asp:Button ID="EditCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel" CssClass="ButtonStyle" TabIndex="100"
ValidationGroup="EditPerson" />
Hope the CausesValidation for the button is set to true.
Well, I fixed the issue. If anyone else encounters this problem try the following:
Ensure there is only one (1) validation group on the entire form.
Set "causesvalidation" to "true" on the submitbutton.
Check, double check and triple check the spelling of your validationgroup property on all relevant controls.
Be on the lookout for javascript errors in your client-side scripts. They can cause havoc.

Categories

Resources