|
 |
|
|
hi, i have a problem with this control. on my page, i use validationgroups. so the normal validationsummary has a validationgroup too. in case of this, the custom errors will not be displayed. has anyone a idea for this problem? i want to set a validationgroup to the customsummary, but it didn't work. thanks for answers
greets joerg
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I may have found a solution to this. I haven't throughly tested it, but prelimenary tests are successful.
[ToolboxData("<{0}:MyValidationSummary runat=server></{0}:MyValidationSummary>")] public class MyValidationSummary : ValidationSummary { /// <summary> /// Allows the caller to place custom text messages inside the ValidationSummary control. /// DummyValidator.ValidationGroup = this.ValidationGroup. /// </summary> /// <param name="message">The message you want to appear in the summary</param> public void AddErrorMessage(string message) { this.Page.Validators.Add(new DummyValidator(message, this.ValidationGroup)); }
/// <summary>Allows the caller to place custom text messages inside the ValidationSummary control.</summary> /// <param name="message">The message you want to appear in the summary</param> /// <param name="validationGroup">User defined validation group</param> public void AddErrorMessage(string message, string validationGroup) { this.Page.Validators.Add(new DummyValidator(message, validationGroup)); }
/// <summary> /// Allows the caller to place custom text messages inside the validationsummary control /// DummyValidator.ValidationGroup = this.ValidationGroup. /// </summary> /// <param name="messages">A list of messages you want to appear in the summary</param> public void AddErrorMessage(IEnumerable<string> messages) { new List<string>(messages).ForEach(delegate(string message) { this.Page.Validators.Add(new DummyValidator(message, this.ValidationGroup)); }); }
/// <summary>Allows the caller to place custom text messages inside the validationsummary control</summary> /// <param name="messages">A list of messages you want to appear in the summary</param> /// <param name="validationGroup">User defined validation group</param> public void AddErrorMessage(IEnumerable<string> messages, string validationGroup) { new List<string>(messages).ForEach(delegate(string message) { this.Page.Validators.Add(new DummyValidator(message, validationGroup)); }); }
/// <summary>Only exists for MyValidationSummary. Always invalid. Inherit CustomValidator to work with ValidationGroup</summary> private class DummyValidator : CustomValidator { /// <summary>Ctor.</summary> /// <param name="errorMessage">error message to display with ValidationSummary</param> /// <param name="validationGroup">always part of the ValidationSummary's ValidationGroup</param> public DummyValidator(string errorMessage, string validationGroup) { this.ErrorMessage = errorMessage; this.ValidationGroup = validationGroup; this.IsValid = string.IsNullOrEmpty(this.ErrorMessage); }
/// <summary>Remove this validator when the page is unloaded.</summary> /// <param name="e">Not Used</param> protected override void OnUnload(EventArgs e) { if (this.Page != null) { this.Page.Validators.Remove(this); } base.OnUnload(e); } } } I made some minor modifications to the code above. 1. added overrides to insert with or without validation group. 2. added AddErrorMessages to insert a collection of error messages. 3. DummyValidator inherits from CustomValidator to access ValidationGroup functionality. 4. DummyValidator is valid if the error message is not null or empty. 5. DummyValidator is an internal class for MyValidationSummary.
Jason Meckley Programmer Specialty Bakers, Inc.
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
seems to be the problem with the "page" variable. scroll down to the error entry which named "error". i think this will solve your problem.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
here are my changes. to show the summary:
ValidationCustomSummary CustomSummary = new ValidationCustomSummary(); CustomSummary.AddErrorMessage(GetGlobalResourceObject("Globals", "passwordChanged").ToString(), "validateOK", this.Page);
ValidationCustomSummary.cs:
using System; using System.Web.UI; using System.Web.UI.WebControls; using System.Collections.Generic;
namespace myControls {
[ToolboxData("<{0}:MyValidationSummary runat="server">{0}:MyValidationSummary>")] public class ValidationCustomSummary : ValidationSummary { public void AddErrorMessage(string message, Page pageRef) { pageRef.Validators.Add(new DummyValidator(message, this.ValidationGroup)); }
public void AddErrorMessage(string message, string validationGroup, Page pageRef) { pageRef.Validators.Add(new DummyValidator(message, validationGroup)); }
public void AddErrorMessage(IEnumerable messages, Page pageRef) { new List(messages).ForEach(delegate(string message) { pageRef.Validators.Add(new DummyValidator(message, this.ValidationGroup)); }); }
public void AddErrorMessage(IEnumerable messages, string validationGroup, Page pageRef) { new List(messages).ForEach(delegate(string message) { pageRef.Validators.Add(new DummyValidator(message, validationGroup)); }); }
public DummyValidator(string errorMessage, string validationGroup) { this.ErrorMessage = errorMessage; this.ValidationGroup = validationGroup; this.IsValid = string.IsNullOrEmpty(this.ErrorMessage); }
protected override void OnUnload(EventArgs e) { if (this.Page != null) { this.Page.Validators.Remove(this); } base.OnUnload(e); } } } }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I've altered the dummy validator class to solve this problem. Instead of implementing the Ivalidator interface just inherit the basevalidator class. This way you can set the validation group property for the dummyvalidator. Usage:
// Create new dummy validator DummyValidator dv = new DummyValidator("Something went wrong");
// Set validationgroup dv.ValidationGroup = "LeadValidators";
// Add validator to collection Page.Validators.Add(dv); The class:
public class DummyValidator : BaseValidator { public DummyValidator(string msg) { ErrorMessage = msg; this.IsValid = false; }
protected override bool EvaluateIsValid() { return false; } } // End of class DummyValidator
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Isn't the best practice to ensure the integrity of the data first before the trip to the server, and also before any erroneous data can possibly be entered ? Take for example a field that must be alpha or numeric only - I can send "123456" or "ABCDEF" and both would be accepted - if for example the data type on the database was char(6) One might ask why would anyone store Numeric only data as char and not as integer - well b/c one might have data with leading zeroes and integer types remove the leading zeroes [01234] is not the same as [1234].
While you certainly can use the sqlserver to provide the summary for the validation the way you have done it - I think that in the long run you will end up in a bad scenario.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
stixoffire wrote: Isn't the best practice to ensure the integrity of the data first before the trip to the server, and also before any erroneous data can possibly be entered ?
yes it is good practice to validate before postback. however, the trade off is code duplication and reuseablitiy. If I place my data validation into my Domain Model, then my validation is in one place no matter where I use the object in the GUI.
if all validation is done on the GUI, how would a shopping cart be validated. you would need to validation the cart was not empty, you would also need to validate the payment, customer, etc. this is better suited for the Domain Model rather than the GUI.
stixoffire wrote: One might ask why would anyone store Numeric only data as char and not as integer - well b/c one might have data with leading zeroes and integer types remove the leading zeroes [01234] is not the same as [1234].
certainly true. although the other argument would be storing the leading zeros is extransious data. Rather this could be a formatting issue which should be dealt with in the either the Domain Model or presentaion layer.
Jason Meckley Programmer Specialty Bakers, Inc.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
| Data with Leading zeroes
"Rather this could be a formatting issue which should be dealt with in the either the Domain Model or presentaion layer."
That would be nice if all data was organized that way - but sometimes when different manufacturers merge with disparate item numbering schemes or schemes that could represent the same item - such as a 4545 being one item and a 04545 being another (sure I would like the easy way - but some People want to make things difficult - (maybe they think difficult is JOB Security)..
I agree with your arguments to the point of where and how the validation is implemented - client side vs. server side - on the client obviously AJAX / Java is good way to go..no postbacks etc.. - I like to create custom controls to do things - so for me - the validation in the custom control (programmability built in of course ) - yes a lot more duplication of effort per control but not per use..lots of pros and cons - could say my controls are bloated..
Thanks for the response..
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi
Thanks for this, it's very useful but it didn't work with me the page is always empty.
this is the code
protected void registerCommand_Click(object sender, EventArgs e) { if (Page.IsValid) { try { Membership.CreateUser(usernameTextBox.Text, passwordTextBox.Text, EMailTextBox.Text);
} catch (Exception E) { // Set Dynamic Error ValidationSummary1.AddErrorMessage(E.Message);
// Redirect } } }
Thanks
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
Instead adding yet another ControlEx to my codelib, i've looked at your code and used it in a different way. I haven't inherited from ValidationSummary, but instead i've just implemented a class that adds a message to the page this.Page.Validators. Doing so I can use the special instance(or static class) when i need to without changing the validationcontrols on my page.
Thanks for the Tip
Simplify is the Max
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
This article helped my thinking alot and provided a solution for my problem, so I thank thee.
I did have trouble with the object reference issue as mentioned by rizob, and his solution worked aswell.
To futher my implementation, i created a new instance of the control on the code-behind page where a validation summary control were already created on the aspx page.
So whenever I get an data driven error/exception, i just pass the error message into the custom validator and the exception is neatly handled!
ah and an example page would have helped aswell 
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I think that it's a big minus that the message box is not functional with this implementation. As far as I've seen the dummy validators are not rendered on the page. If the message box is a client script (must be) than it won't find these dummy validators... (probably this is what happens there)
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
It took me awhile to figure out how to use the ValidationSummary. I kept trying to instantiate one on the page. It finally occured to me to add one to the page dynamically. I think a sample showing this would be useful.
Randall Moore http://www.IssueView.Com
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Hi. I have created a class using the provided code. I get the error 'object reference not set to an instance of an object'. It seems to be occurring on this line. this.Page.Validators.Add(new DummyValidator(msg)); I created an instance to the DummyValidator and commented out the above line and got no error. So, it is clearly not referencing the 'Page' correctly. Any ideas on where I am going wrong.
Thanks.
rizob
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
solved. here's what i did in case anyone else encounters this problem. in my calling page i created a reference to the page. 'system.web.ui.page pageRef = this.page;' add this to the class method. 'AddErrorMsg(string msg, system.web.ui.page pageRef)' then set a variable in the class. 'system.web.ui.page mypageRef = pageRef' finally use the variable to 'mypageRef.validators.add(DummyValidator(msg));'
code works great now. thanks.
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
solved. here's what i did in case anyone else encounters this problem. in my calling page i created a reference to the page. 'system.web.ui.page pageRef = this.page;' add this to the class method. 'AddErrorMsg(string msg, system.web.ui.page pageRef)' then set a variable in the class. 'system.web.ui.page mypageRef = pageRef' finally use the variable to 'mypageRef.validators.add(new DummyValidator(msg));'
code works great now. thanks.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
...once I've added it to the summary, it seems to prevent postback when all other validator controls are "happy". And since my logic as to whether to display the message is here I'm stuck When I invalidate any other validation control I can then postback after correcting it
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
this is a handy trick. i just have one question, how come I can't get the messagebox to show up? I have showmessagebox = true but it is not showing. i wanted to try your example because I couldn't get the messagebox to show up for a customvalidator control either. any suggestions. Thanks for the help.
Randy
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |