validation control in asp.net with masterpage buttons - asp.net

I have a lot of textboxes on submit page and all have a validator. I want to control them with a submit button which is on master page. It works but other buttons also cause validation. validation summary is always shown either submit button is clicked or any other buttons. how can I control this situation? (asp.net)

for each individual submit use "ValidationGroup". here is the sample:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="TextBox1" ValidationGroup="Submit" ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="TextBox2" ValidationGroup="Submit" ID="RequiredFieldValidator2" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" ValidationGroup="Submit" runat="server" Text="Button" />

I think you can resolve this by using ValidationGroup Property..
http://msdn.microsoft.com/en-us/library/ms227424.aspx
http://www.w3schools.com/aspnet/prop_webcontrol_imagebutton_validationgroup.asp
Thanks
Deepu

Related

list of all validation errors on page post back including hidden fields in multiview

I am working on an .Net Webforms project and on one page there is a multiview that contains many fields that need validation on each view. All have required field validators, but I need to only validate when the user wants to update the saved data, not when they are just looking at it. Because of this the client side doesn't know of the existence of fields from view1 in the multi view when view 5 is the only one visible. Page.isvalid returns false but I need to display all there errors in a validation summary.
I am not sure what you really want, I think you mean that you want to display all errors from all the "view" in the ValidationSumary?
Some note: using ValidationGroup for all controls, buttons and validationsumary, put validationsumary outside the views...
The best is to show your code here, but here is a sample of working code that may help you, get this from here
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View ID="View1" runat="server">
view 1: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="RequiredFieldValidator1"></asp:RequiredFieldValidator>
</asp:View>
<asp:View ID="View2" runat="server">
view 2 :<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2" ErrorMessage="RequiredFieldValidator2">*</asp:RequiredFieldValidator>
</asp:View>
</asp:MultiView>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="submit" />
<asp:Button ID="Button2" runat="server" Text="view2" onclick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="view1" onclick="Button3_Click" />

Required Field Validator on FileUpload Control

I have a form in which I have two textboxes and one fileupload control, I am using required field validator on one textbox and on Fileupload control, When I am clicking the submit button, its disabling the fileupload control and not showing any validation for it.
I also have second button for Cancel, clicking on which redirects to previous page, when I am clicking this button its also disabling the fileuploadcontrol.
Below is my code
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:RequiredFieldValidator ID="rfvFileupload" ValidationGroup="validate" runat="server"
ControlToValidate="FileUpload1"></asp:RequiredFieldValidator>
<asp:TextBox ID="txtSubj" runat="server" ></asp:TextBox><asp:RequiredFieldValidator
ID="rfvSubject" ControlToValidate="txtSubj" runat="server" Display="Dynamic"
EnableClientScript="true" ErrorMessage="* required" ValidationGroup="validate" />
<asp:Button ID="btnupload" runat="server" Text="Send" ValidationGroup="validate"
OnClick="btnupload_Click">
<asp:Button ID="btncancel" runat="server" Text="Cancel" OnClick="btncancel_Click"
/>
Try using no ValidadationGroup:
<asp:FileUpload ID="fupDocument" runat="server" Width="100%" />
<asp:RequiredFieldValidator runat="server" Display="Dynamic" ErrorMessage="* Required Field" ControlToValidate="fupDocument">
</asp:RequiredFieldValidator>
I tried and work it.

Validation Group in ASP.NET 1.1

I have 2 buttons which validates different sets of textboxes (which have corresponding asp validators). How can I control the validations triggered by each button?
Add ValidationGroup="set1" and ValidationGroup="set2" properties to your textboxes, buttons, Validators and Validation Summaries, that should do it.
You have to set the same Group name for each set of textbox and buttons .
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="GroupOne"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1"
ErrorMessage="ErrorMessageForTextBox1" ValidationGroup="GroupOne">
</asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="GroupOne" />
<asp:TextBox ID="TextBox2" runat="server" ValidationGroup="GroupTwo"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2"
ErrorMessage="ErrorMessageForTextBox1" ValidationGroup="GroupTwo">
</asp:RequiredFieldValidator>
For ASP version 1.1 , use this link !

How to limit validation to a particular trigger?

Is there any way to limit the validation of a validator control to a particular event or trigger? Say, I want my validator1 to be activated only when button1 was clicked, and validator2 to be activated only when button2 was clicked. How can I do this in asp.net?
ValidationGroups are what you're looking for(ASP.NET >= 2.0)
Validation groups allow you to organize validation controls on a page
as a set. Each validation group can perform validation independently
from other validation groups on the page.
<asp:textbox id="AgeTextBox"
runat="Server">
</asp:textbox>
<asp:requiredfieldvalidator id="RequiredFieldValidator1"
controltovalidate="AgeTextBox"
validationgroup="PersonalInfoGroup"
errormessage="Enter your age."
runat="Server">
</asp:requiredfieldvalidator>
<br /><br />
<!--When Button1 is clicked, only validation
controls that are a part of PersonalInfoGroup
are validated.-->
<asp:button id="Button1"
text="Validate"
causesvalidation="true"
validationgroup="PersonalInfoGroup"
runat="Server" />
<br /><br />
<asp:textbox id="CityTextBox"
runat="Server">
</asp:textbox>
<asp:requiredfieldvalidator id="RequiredFieldValidator2"
controltovalidate="CityTextBox"
validationgroup="LocationInfoGroup"
errormessage="Enter a city name."
runat="Server">
</asp:requiredfieldvalidator>
<br /><br />
<!--When Button2 is clicked, only validation
controls that are a part of LocationInfoGroup
are validated.-->
<asp:button id="Button2"
text="Validate"
causesvalidation="true"
validationgroup="LocationInfoGroup"
runat="Server" />

Validating controls in ListView insert/edit templates

I added a RequiredFieldValidator to my InsertItemTemplate, and it seems to be working fine. The problem I am having, however, is that now I cannot do anything else in the ListView (like edit or delete items) UNLESS the required field has a value. Is there some way I can manually do the validation when the user clicks the 'Insert' button on the InsertItemTemplate, or some other little trick I can perform so the user doesn't have to first type in a value just to delete something else in the list?
Thanks
A_Nablsi,
Please provide the code for your solution to turn the Insert New validation controls off when in Edit/Update mode or turn Edit/Update validation controls off when both the Edit and Insert Rows are active at the same time. This code using your notional solution fails with a null reference to the updateButton.
LinkButton updateButton = LVTasks.EditItem.FindControl("UpdateButtonTask") as LinkButton;
updateButton.CausesValidation = false;
The solution that works is adding Validation Groups.
Include ValidationGroup="myVGEdit" with your Validator Control(s) in the EditItemTemplate and your Update button.
Include ValidationGroup="myVGInsert" with your Validator Control(s) in the InsertItemTemplate and your Insert button.
<asp:ListView ID="LVTasks" runat="server"
DataKeyNames="IDTask"
DataSourceID="LDS_LVTasks"
InsertItemPosition="FirstItem"
oniteminserting="LVTasks_ItemInserting"
onitemupdating="LVTasks_ItemUpdating"
onitemcommand="LVTasks_ItemCommand"
>
<EditItemTemplate>
<asp:TextBox ID="TaskUpdateTextBox" runat="server"
Text='<%# Bind("Task") %>'
TextMode="MultiLine" Rows="1" Font-Bold="true" Width="300px"
/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please Set Task title"
ControlToValidate="TaskUpdateTextBox"
ValidationGroup="myVGUpdate"
/>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Cancel"
CausesValidation="False"
ToolTip="Cancel - Abort - No Changes"><div class="Cancel"></div></asp:LinkButton>
<asp:LinkButton ID="UpdateButtonTask" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Update"
CausesValidation="True"
ValidationGroup="myVGEdit"
ToolTip="Save Changes - Update"><div class="Update" ></div></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TaskInsertTextBox" runat="server" Text='<%# Bind("Task") %>'
TextMode="MultiLine" Rows="1" Font-Bold="true" Width="300px"
/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please Set Task title"
ControlToValidate="TaskInsertTextBox"
ValidationGroup="myVGInsert"
/>
<asp:LinkButton ID="CancelButton" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Cancel"
CausesValidation="False"><div class="Clear" ></div></asp:LinkButton>
<asp:LinkButton ID="InsertButtonTask" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Insert"
CausesValidation="true"
ValidationGroup="myVGInsert"
><div class="Insert" ></div></asp:LinkButton>
</InsertItemTemplate>
Yes,
set the CausesValidation property to false on the controls you don't want them to trigger the validation.

Resources