.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

How to use ValidationSummary

Posted By:      Posted Date: October 01, 2010    Points: 0   Category :ASP.Net

Hi All

I am trying to do a simple validation where i check the text box value. I have used follwing approch.

<%: Html.ValidationSummary(true) %> in View.

[Required (Error Message="Name Required")]

public virtual string Name{get;set;}

but in my controller if i enter a blank value in txt box then still the ModelState.IsValid is property is etting true.


What could be the problem please help.






View Complete Post

More Related Resource Links

Inserting personal error message in ValidationSummary



I am creating a web form that requires a lot of validation. I use many of the validation control in asp.net already like RequiredFieldValidator and CompareValidator. 

My Problem: after my custom validation for the value entered return an error message, and I pressed enter key when the field is empty, my custom error message is overlapped by my ValidationSummary. 

Setting my custom error to visible = false on button click also does not seem to work as it requires for me to enter the value first.

This made me think, is there a way to insert my custom validation inside the ValidationSummary as an error message like for other validation control?

Thanks in advance

ASP.NET MVC with JQuery Validation and ValidationSummary



          One of the great feature of ASP.NET MVC 2 is the support of client side validation. Client side validation

ValidationSummary on MVC

Hi friends I want to validate my different pages so I use System.ComponentModel.DataAnnotations to do that But I have a problem with that:  ValidationSummary.cs: namespace KISP.Ensync.Models { public class ValidationSummary { [Required, Range(0, 200)] public int? Age { get; set; } [Required, RegularExpression(ValidationRules.Controls["login.userName"].RegEx, ErrorMessage = ValidationRules.Controls["login.userName"].ErrMsg)] public string Name { get; set; } } } and I have a class ValidationRules where I have a dictionary for each regular expressionand the error message: namespace KISP.Ensync.ControlValidation { public static class ValidationRules { public static Dictionary<string, ValidationItem> Controls = new Dictionary<string, ValidationItem>() { {"login.userName", new ValidationItem(".*", "Invalid character in user name")}, {"login.userPass", new ValidationItem(".*", "Invalid character in user password")} }; } public class ValidationItem { public string RegEx; public string ErrMsg; public ValidationItem(string RegEx, string ErrMsg) {[Required, RegularExpres


if (RadioButton1.Checked == true)            {                ValidationSummary RegularExpressionValidator5 = LoginView2.FindControl("RegularExpressionValidator5") as ValidationSummary;                ValidationSummary RequiredFieldValidator3 = LoginView2.FindControl("RequiredFieldValidator3") as ValidationSummary;                ValidationSummary RegularExpressionValidator1 = LoginView2.FindControl("RegularExpressionValidator1") as ValidationSummary;                ValidationSummary RegularExpressionValidator2 = LoginView2.FindControl("RegularExpressionValidator2") as ValidationSummary;                ValidationSummary RegularExpressionValidator3 = LoginView2.FindControl("RegularExpressionValidator3") as ValidationSummary;                ValidationSummary RequiredFieldValidator2 = LoginView2.FindControl("RequiredFieldValidator2

Clearing ValidationSummary when form returns a download request



Here's my setup:

Simple mvc page with a few controls that includes a validation summary area. 

Once the fields are completed there's a download button.

If I enter something invalid, as expected, my controller code updates the UI with the appropriate error.  However, if I then correct the error, and then click the download button a file is downloaded, but the UI never gets updated - presumeably since there's no update on the view.

Any ideas on a way around this - perhaps some javascript code.  Just wondering what's the best practice here?


ValidationSummary Bulletlist mode in CreateUserWizard




On the following code, when I clicked submit, why the validation summary does not use the Bulletlist mode to show errors?



<%@ Page Language="VB" debug="true"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script runat=server>
Sub checkUsername(ByVal sender As Object, ByVal args As ServerValidateEventArgs)

        Dim str As String = args.Value
        Dim myBool As Boolean = String.IsNullOrEmpty(str)
        args.IsValid = False

        If args.Value = String.Empty Then
            args.IsValid = False
            args.IsValid = True
        End If
    End Sub

<form id="form1" runat="server">
<asp:CreateUserWizard ID="CreateUserWizard1" Runat="server">
  <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
        <table border="0" style="font-size: 100%; font-family: Verdana">

Show MessageBox of ValidationSummary from Codebehind



 I want to Show MessageBox of the ValidationSummary (ShowMessageBox="true")  from Code behind. Below is the code i used.Any tips.

Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
 Dim blnFlag = callmethod()
 If Not blnFlag Then
    ValidationError.Display("Place error message here")
 End If

End Sub

Public Class ValidationError
    Implements IValidator
    Private _errorMessage As String = String.Empty
    Private _isValid As Boolean = False
    Public Shared Sub Display(ByVal message As String)
        Dim currentPage As Page = TryCast(HttpContext.Current.Handler, Page)
        currentPage.Validators.Add(New ValidationError(message))
    End Sub
    Public Sub New(ByVal message As String)
        ErrorMessage = message
        IsValid = False
    End Sub
    Public Property ErrorMessage() As String Implements System.Web.UI.IValidator.ErrorMessage

ValidationSummary and Scroll Position



I have a form which has a number of user controls on it.  Each user control has a Validation Summary inside it, and if a user clicks on the the submit button for a particular User Control, the user control validates itself and shows errors in the Validation Summary.

The problem is that if any error show up, the page returns to the top of the page.  Is there any way of making sure that this doesn't happen?

Please don't advise me to use Page.MaintainScrollBackPosition because I already use this and this only helps when a post back occurs.  Because the validators execute on the client side, there is no post back.  I'm guessing there is a problem (?) with using ValidationSummary that causes the page to scroll up to the top of the page?

ValidationSummary control messages


I know this question has been asked before, and nobody has explained the reason for it....

When my validation fires on my web form, the validation summary writes out the same error message for each control that was in error twice! I have no validation groups in my web form.

Here is my html for the Validationsummary control.


<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="BulletList"
        ShowMessageBox="true" ShowSummary="false" EnableClientScript="true" HeaderText="You must enter a value in the following fields:" />


A typical control is set up below as follows:


<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="HospAcctno"

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend