Link to home
Start Free TrialLog in
Avatar of katamatic
katamatic

asked on

How to programatically append to existing ValidationSummary (VB solution)

Hi everyone - not sure how to accomplish this - I hope someone can help (VB please).

I have a database insert (via a DetailsView) which uses the standard ASP.NET 2 validation controls, i.e.

asp:RequiredFieldValidator       to pinpoint what to validate (and specify the text for an associated error), and

asp:ValidationSummary        to collate and display all the error message from the RequiredFieldValidators

This worked fine - but now I have a dynamically generated checkboxlist for which I need to check at least one field is selected (and of course .NET validation doesn't cater for checkboxlists).

I'm actually already calculating how many of the checkboxes have been ticked, so identifying a zero result isn't the problem - what I'd like to do (and don't have a clue how) is incorporate an error message resulting from such a zero result in the existing 'ValidationSummary' (i.e. so it appears at the same time, as part of the same list).

Thanks for looking.
Avatar of mrichmon
mrichmon

Very easy.

Just add a validator to the page and it will automatically add to the validation summary.  Here is code (C#), but it should be similar:

CustomValidator MyValidator = new CustomValidator();
MyValidator.ErrorMessage = "Please select at least one checkbox";
MyValidator.IsValid = false;
MyValidator.ID = "checkboxvalidator";
this.Page.Validators.Add(MyValidator);
Avatar of katamatic

ASKER

Hi - many thanks for getting back to me.

However, I'm having problems implementing your solution for three reasons;

1. I'm not an accomplished coder :(
2. When I do write code for ASP.NET it is always in VB (so you C# code looks 'kinda' familar but I don't really understand it).
3. I'm not sure where and how this would be implemented in the page - in a Sub routine?

So in summary - apologies for being stupid - but I don't get it ! (And I was so happy when your opening words were 'very easy' !)

If you could give me a few more pointers I'd be very grateful.
It would be done wherever you have the custom code to identify how many boxes are ticked.  If none are ticked then execute that code.  So wherever that code is executed to test the number that are ticked - this code would be part of that.

Let me see if I can translate to VB for you (I am not used to VB as I use C#)...

Dim MyValidator as CustomValidator
MyValidator.ErrorMessage = "Please select at least one checkbox"
MyValidator.IsValid = false
MyValidator.ID = "checkboxvalidator"
this.Page.Validators.Add(MyValidator)

try that - I hope that does it.
If that doesn't work then at least the first line should to declare the variable.  Then hit the . which should get intellisync for you for the syntax for the next few lines.

The concept is identical it is just the syntax that may be a bit different (like no semi-colons in VB and Dim to declare a variable).
Is this asp:RequiredFieldValidator being generated in datagrid/gridview? Also see if any of the following post helps:

Adding Validator Control at runtime in a Custom Control
http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet.buildingcontrols/browse_thread/thread/a1104f33f115533b/b7032f11bde9363b?lnk=st&q=add+RequiredFieldValidator+runtime&rnum=1&hl=en#b7032f11bde9363b

--Nauman.
Hi mrichmon (and also Nauman).

mrichmon - many thanks for you continued help - I'm embarrassed to say I'm still having no joy. (Nauman - an interesting link - close to - but not quite what I needed since it is C# - although it did point me in the direction of using the OnInit event).

Back to mrichmon -

Hi - I'm getting closer - I can now see how your suggested code will work but I'm still struggling to implement - again this is down to my limited coding skills. (I see similar questions posted elsewhere on the web but every single solution is C# - maybe I should think about learning c# :(  )

I think I need to call this code in the OnInit event handler of the page but I'm unclear what VB syntax I need to wrap your suggested code in.

(i.e. I guess it needs to start something like . . . Sub Page_OnInit  . . . but the rest is a mystery).

I you can give me a clue that would be great (although I appreciate VB isn't your bag).

If not - no worries.

 
>>I think I need to call this code in the OnInit event handler of the page
I don't think so.

You mentioned previously:

>>I'm actually already calculating how many of the checkboxes have been ticked, so identifying a zero result isn't the problem

Where are you doing that?
This code can be added immediatly following that code.

In general it would probably  be in either the Page_Load in an if statment tetsting if the page posted back OR in the function that your submit button submits to.  Then you do the test to see how many are ticked.  If 0 then run the few lines of code I showed.  Then test if the page is valid.

Hope that helps.

if not it would help to see the code you have...
Hi mrichmon

Once again - thanks for taking the time to continue to look at this for me.

OK - I'm still struggling. However, rather than overcomplicate things by posting all my code and then trying to explain why I'm doing various things, I thought it would be easier to keep this as simple as possible and just highlight my particular problems.

1. I've simplified things as a test.

2. I've built a simple page with a DetailsView insert.

3. I've added a single 'RequiredFieldValidator' to one of the DetailsView fields and a 'ValidationSummary' at the top of the page to display the error message.

4. When tested this works as expected.

5. Using the Page_Load event I programatically add a 'CustomValidator' using VB and the code you suggested (I had a little trouble with the VB syntax but the following runs without error);

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim MyValidator As New CustomValidator
        MyValidator.ErrorMessage = "This is the error message"
        MyValidator.IsValid = False
        MyValidator.ID = "testvalidator"
        Validators.Add(MyValidator)
    End Sub

(I realise this example always would always sets the status to 'False' but this is fine for test purposes.)

6. Whilst the new 'MyValidator' IS created it does not function entirely as expected. (Or at least as 'I' expected!)

7. The page loads - and immediately the 'This is the error message' is displayed by the 'ValidationSummary'. However, the error message from the 'RequiredFieldValidator' within the DetailsView does not appear. (Whilst I would not expect the 'RequiredFieldValidator' to display a validation error message until the page is submitted I was surprised the same was not true of the Custom error message.)

8. If I then attempt to submit the page with the 'RequiredField' left blank I get the appropriate error message - but not the custom message (I would have expected both to be fired).

9. Finally, if I then complete the required field and submit, the custom verification does not trigger and the page is submitted.


So to sum up. Whilst I can now create the CustomValidator programatically with the code you suggest I'm struggling to get the Custom verifaction to fire simultaneously with the non-custom verification.

Any further suggestions would be great.
SOLUTION
Avatar of nauman_ahmed
nauman_ahmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Gentlemen - thanks for your latest posts.

Unfortunatley, I have not had the opportunity to pursue this problem and will not for the next few days. However, rest assured I am confident your final comments will solve my problem - as soon as I have done so I will post the details and award the points.

Until then, once again my thanks for your ongoing help.
Just got the email.

I think a split between myself and nauman would be more appropriate
A delete refund is a bad option considering the user stated they thought our solutions would work and they would be awarding points.