Solved

Required field validation in grid .

Posted on 2011-03-24
3
693 Views
Last Modified: 2012-05-11

I have a grid with a textbox and dropdown template fields and I have a required field validator for each. The gridview contains 15 rows.

How can I make the validation appear when the save button is clicked, just for some specific rows and not for all rows?

Any Solutions.
0
Comment
Question by:rohitnet100
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
SAMIR BHOGAYTA earned 500 total points
ID: 35212313
Hi, use this code

   1. For each of your existing RequiredFieldValidators that display the "Please provide text for 'Name' field" message you will need to set:
          * EnableClientScript="false"
          * ValidationGroup="vgTxtName" (provide your own name)
          * ErrorMessage="" (or remove it altogether; the CustomValidator will now be responsible for this)
      You have the option of displaying nothing at all (less clear to the user) or displaying an asterisk to indicate which validator is invalid.
      Option 1:
          * Display="None"
      Option 2 (preferred):
          * Display="Dynamic"
          * Set the text in between the validator tags to: *
   2. No changes needed for your ValidationSummary control (it should be neutral and not have a ValidationGroup attribute set, which is the default)
   3. Add a CustomValidator (see code below)
          * Add an eventhandler for the CustomValidator's ServerValidate event (you can just double click it from the designer to have it generated)
          * Implement the eventhandler logic (see code below)

The idea is not to directly allow the page to handle those RequiredFieldValidators anymore and instead we'll let the CustomValidator do it.

TextBox RequiredFieldValidator example (you should have something that looks like this with relevant ID names which corresponds to step 1 above):

Option 1:

<asp:RequiredFieldValidator ControlToValidate="txt1" ID="rfv1" runat="server"
EnableClientScript="false" Display="None" ValidationGroup="vgTxtName" />

Option 2:

<asp:RequiredFieldValidator ControlToValidate="txt1" ID="rfv1" runat="server"
EnableClientScript="false" Display="Dynamic" ValidationGroup="vgTxtName">*
</asp:RequiredFieldValidator>

CustomValidator Markup (you can place this anywhere sensible, such as next to the ValidationSummary control):

<asp:CustomValidator ID="cvName" runat="server" Display="None"
ErrorMessage="Please provide text for 'Name' field"
OnServerValidate="cvName_ServerValidate" />

The error message here replaces the ones from the individual validators. Also notice there's no ControlToValidate set, which is valid for this type of validator and is useful for applying validation covering multiple controls.

CustomValidator EventHandler (cvName_ServerValidate):

protected void cvName_ServerValidate(object source, ServerValidateEventArgs args)
{
    // Validate vgTxtName group
    Page.Validate("vgTxtName");

    // .NET 3.5 - add using System.Linq;
    args.IsValid = Page.GetValidators("vgTxtName")
                        .OfType<RequiredFieldValidator>()
                        .All(v => v.IsValid);

    // .NET 2.0 (use either this or the above, not both)
    bool isValid = true;
    foreach (RequiredFieldValidator validator in Page.GetValidators("vgTxtName"))
    {
        isValid &= validator.IsValid;
    }
    args.IsValid = isValid;
}
0
 

Author Closing Comment

by:rohitnet100
ID: 35215402
thanks much
0
 

Author Comment

by:rohitnet100
ID: 35215424
Also, how to achieve if i want to make validation appear when the save button is clicked, just for some specific columns and not for all columns?
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Hide Tab Page 3 18
what technologies offer Authentication over Web Services? 4 55
onOpen 14 38
Getfiles in vb.net 21 12
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now