ASP C# RequiredFieldValidator - c#

[textbox1]
[textbox2]
[button1] [linkbutton1]
i have 2 textboxes, 2 RequiredFieldValidator, button1 and linkbutton.
the button1 and 2textboxes are working properly with RequiredFieldValidator, but if i click the linkbutton the RequiredFieldValidator also appears.
I want the RequiredFieldValidator focus only in button1. Is this possible?

Set CausesValidation="false" in your link button markup like this
<asp:LinkButton ID="lnkButton" runat="server" CausesValidation="false"></asp:LinkButton>

you need to specify Validation Groups
<asp:requiredfieldvalidator id="RequiredFieldValidator2"
controltovalidate="AgeTextBox"
validationgroup="PersonalInfoGroup"
errormessage="Enter your age."
runat="Server">
</asp:requiredfieldvalidator>

Use ValidationGroup property of RequiredFieldValidator.
Set the ValidataionGroup="someName" for your textbox1-requiredfieldvalidator, textbox2- requiredfieldvalidator and your button1 control.
Note: Keep the name of the ValidationGroup same.

Related

Validation effects the entire form, no button can be pressed

I've got a page that contains a huge amount of controls, (gridviews, reportviewers, buttons etc.)
Now I've a couple of textboxes and dropdowns that represent a "form" user might want to fill up and submit by clicking a button. Some of these controls contain "RequiredFieldValidator"s.
<fieldset runat="server" >
<asp:Label ID="lblName" runat="server" Text="Name:" AssociatedControlID="txtName" />
<asp:RequiredFieldValidator ID="rfvTxtName" runat="server" ControlToValidate="txtName" Text="(Required)" />
<asp:TextBox ID="txtName" Width="175px" runat="server" />
....
....
</fieldset>
The problem I encounter is that no other button on the entire asp form can be clicked because of these validation controls.
The question is; how do I set this validation to only listen to that particular fieldset and not the whole page?
Set the ValidationGroup property of those controls to something unique.
More info here.
Set your controll's ValidationGroup property to make them one group . Like
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button"
ValidationGroup="MyGroup" />
The ValidationGroup property specifies which group of controls is
validated on validation. This property is mostly used when there are
several buttons in a form.
ASP.NET ValidationGroup Property
You ca also change property CausesValidation to False for control that you don't want to validate.

Change the ControlToValidate of RequiredFieldValidator dynamically

I have a textbox and dropdown inside a <td> based on certain condition I'll have to hide the text box and show the dropdown. If dropdown is visible is it possible to use the same requiredfield Validator. Is this possible?
<td>
<asp:TextBox ID="txtLimit" runat="server" />
<asp:DropDownList ID="ddlCurLiabiltyLimits" runat="server" CssClass="TextNormal" OnSelectedIndexChanged="ddlCurLiabiltyLimits_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtLimit" ErrorMessage="Answer is required." ClientIDMode="Static" />
</td>
In my code behind :
RequiredFieldValidator1.ControlToValidate = ddlCurLiabiltyLimits.ID ;
but this doesn't seem to be working.
One thing that might be causing you problems is when using RequiredFieldValidator on a DropDownList, you will want to specify the RequiredFieldValidator.InitialValue property to indicate the Value of the "first" item in the DropDownList. Maybe that is where your problem is?
My guess is that you might be assigning the ID to the ControlToValidate property too late in the page lifecycle. Is it possible to assign this during the page's OnInit event?
It might be easier to just use a second RequiredFieldValidator for the DropDownList.

how to give required field validator to textbox in gridview?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Height="146px"
Width="308px">
<Columns>
<asp:TemplateField HeaderText="Original Price" ControlStyle-Width="100px">
<ItemTemplate>
<asp:TextBox ID="txtOriginalPrice" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtOriginalPrice"
ValidationGroup="GridView1" Display="Static" ErrorMessage="" Text="*"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I am using above code but its not working though I have put requirefield validator it does not show me the '*'
Yes, your requiredfieldvalidator is certainly there, and it knows which control to validate.What is missing is "when to validate that control".And to answer this question you need to add your textbox the same ValidationGroup with your requiredfieldvalidator and also the control(this can be a button for example) causes to do a validation.So your code will be like
<%--<asp:Button ID="Button1" runat="server" ValidationGroup="GridView1" Text="Benjamin"...Somewhere in your code--%>
<asp:TextBox ID="txtOriginalPrice" runat="server" ValidationGroup="GridView1"></asp:TextBox>
So don't forget these question
What to validate?(a textbox)
When to validate?(after a button click)
After what action try to validate?(a button click)
With what to validate?(a requiredfieldvalidator)
All of these controls must have the same ValidationGroup.
In case you need the validation to be performed then check for something like this
<asp:Button ID="btnAdd" runat='server' ValidationGroup='GridView1' CausesValidation='true'.....
So now when you click the add button it will validate for those controls falling under the validation group you mentioned else the default value is "" hence you won't find any validation triggered.
Add ValidationGroup to TextBox (txtOriginalPrice), Button and other controls.

Required field validator stopping image button onclick?

I'm having an issue with an image button that I have built. When I attach required field validators to the page, they stop the button onclick event from firing. I am pretty perplexed by this as I can't see any issues in my code!
Please could you cast your eyes over this and help me out?
Cheers
<asp:TextBox ID="TB_Newsletter" runat="server" CssClass="nwsltr-input"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="V1" runat="server" Display="Dynamic" ControlToValidate="TB_Newsletter" ErrorMessage="You must enter your email address"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" ValidationGroup="V1" Display="Dynamic"
ValidationExpression="^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*#([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"
ErrorMessage="Invalid Email Address" ControlToValidate="TB_Newsletter"></asp:RegularExpressionValidator>
<asp:ImageButton ID="IB_SubScri"
ImageUrl="~/_includes/images/buttons/nwsltr-btn.png" runat="server"
onclick="IB_SubScri_Click" CausesValidation="True" ValidationGroup="V1"/>
When you press the button it submits the form, but prior to that the field validators are firing by script - the form won't post if validation fails. <asp:Imagebutton /> and <asp:Button /> types allow you to disable validation when they are pressed:
<asp:ImageButton ... CausesValidation="False"/>
From MSDN on the CausesValidation property:
true if the control causes validation
to be performed on any controls
requiring validation when it receives
focus; otherwise, false. The default
is true.
See this MSDN reference for more information.
Obviously, we assume here that the validator firing when pressing this button is not required.
Change "CausesValidation" to False on your ImageButton

Validation in imagebutton, button not occuring

In my project, validation is not occurring at button_click and imagebutton_click. Why could it be so?
I also gave same validation group to all validators and button and also CAUSEVALIDATION "true" in button
Please help solve this.
Add validation group to image button too and check. Also check if your image button has image in it. Other wise it will not fire events
**
<asp:TextBox ID="txt_password" runat="server" CssClass="text_box_password" TextMode="Password" Width="180px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txt_password" ErrorMessage="*" ValidationGroup="aaaa">**</asp:RequiredFieldValidator>
**

Categories

Resources