Link to home
Start Free TrialLog in
Avatar of Ken Fayal
Ken FayalFlag for United States of America

asked on

How do I access validation controls programmatically in GridView EditItemTemplate?

Hello experts!

Working on a DotNetNuke module, but I think this is an ASP.NET question.

I have a bunch of validation controls on my GridView where their Display property=Dynamic.  I noticed that I am not able to access these controls programmatically even if I use the FindRecursiveControl() function available on the Internet.  I don't want to hard code the ErrorMessage property so I'd rather load these all from my resource .RESX file when the page loads.  I have also tried to declare the resourcekey attribute on the validation control to no avail.

I already know how to assign my string value to the ErrorMessage property, the problem is I cannot instantiate my validation controls.  I know I'm missing the event where I need to do it, but Page_Load and Page_Init do not "see" them.  

I can "see" the controls in the LinqDataSource Updating event, but by the time it gets to that event, it is too late to assign the ErrorMessage property.  I have to catch it somewhere before.  I have also tried the RowDataBinding event, but the controls are not rendered there either.

Below is a sample of one of the EditItemTemplates in my GridView.  Notice that I have to hard code the ErrorMessage property.

I've also included how I'm doing the localization string assignment. The string retrieval code on the right side of the equal sign is working.  It's finding the instance of reqFldVal4 that is the problem.  As I mentioned before, I have tried the FindControlRecursive function as well shown below.

// Sample EditItemTemplate code in .ASPX
<EditItemTemplate>
    <asp:TextBox ID="TextBox4" runat="server" CssClass="inputTextStyle" Text='<%# Bind("Deposit_Date", "{0:MM/dd/yyyy}") %>'  Width="75px" MaxLength="10"></asp:TextBox>
    <asp:RequiredFieldValidator ID="reqFldVal4" runat="server"          ControlToValidate="TextBox4" CssClass="validationRed" Display="Dynamic" ValidationGroup="editPayment"
                    ErrorMessage="Deposit Date is required">*</asp:RequiredFieldValidator> <asp:RegularExpressionValidator ID="regexVal2" runat="server" 
                    ControlToValidate="TextBox4" CssClass="validationRed" Display="Dynamic" ValidationGroup="editPayment"
                    ErrorMessage="Deposit Date is required to be valid date" 
                    ValidationExpression="\b(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2}\b">*</asp:RegularExpressionValidator>
</EditItemTemplate>
 
// Code behind - trying to load the string from the resource file.
reqFldVal4.ErrorMessage = Localization.GetString("DepositDate_Req_Validation.ErrorMessage", LocalResourceFile);
 
// Also tried this:
 
(RequiredFieldValidator) rfv = (RequiredFieldValidator)FindControlRecursive(pmtGrid, "reqFldVal4");
rfv.ErrorMessage = Localization.GetString("DepositDate_Req_Validation.ErrorMessage", LocalResourceFile);
 
// rfv seems to come up null as a result of the FindControlRecursive function therefore the subsequent line of code fails.

Open in new window

Avatar of tillgeffken
tillgeffken

You could add your validator programmatically in the RowDataBound event.

http://www.aspdotnetcodes.com/GridView_Dynamic_Validation.aspx
Avatar of Ken Fayal

ASKER

I have considered doing this but I'd like to be able to see if I can touch the declared validators first.
ASKER CERTIFIED SOLUTION
Avatar of tillgeffken
tillgeffken

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
tillgeffken,
Thanks again for the response.  I have tried this in the RowDataBound event.  The validation controls are not found in this event.  In other words, the rfv variable is null and therefore line 6 of your example fails with the standard "Object not set or blah blah" error.  Even when I do a controls.Count on that cell, I never find any more than a label and a couple of literal controls.  

However, when I do the EXACT same code in the RowUpdating event, the validation controls are found, but it is too late to assign anything to the ErrorMessage properties.  

If I change the .Display property of the validation controls to "Static" THEN the code in your example and in my original question posting, works.  But I don't want to have any static controls on the page since these edit fields on the grid view are in tight proximity to each other.

This is very perplexing.
Did you try hooking into the RowCreated Event?
No, I hadn't tried that but I just did and all I see are my label and textbox controls, no validation controls.  This looks more and more like I'm going to have to programmatically put these validation controls on the page.  Dang.  
Well, while working on another issue, I found out what I have to do to make this work.  Inside the RowDataBound event, I ALSO have to do the following test:

Wow.. that was a troublesome task.


if (e.Row.RowState == DataControlRowState.Edit)
{
    // Populate my resource strings on the validators here
}

Open in new window

But it makes perfect sense. Glad you worked it out.
Giving you the points since you stuck with me on it.
Thanks for awarding me the points.

However accepting my comment as solution is not an option as it is clearly not the solution to your problem and might confuse other readers. Not sure if it's possible but If you insist on awarding me the points could you accept your own comment as solution and my comment as assist and split the points accordingly? For what it's worth your last comment should end up to be the solution in order to be useful for other readers.
I did try to accept my own comment as the solution, but when I did that the system informed me that the issue was going to be closed.  I'm hoping that someone reading all they way through the thread would see what is going on.
I'd say this is a major design flaw in Experts Exchange. I could repost your comment and you could accept that maybe? That would at least help others to find the correct one.
I'm told that the question is now closed and the option to accept the solution has been removed.
Usually there's an option for experts to object against the decision but since you accepted my comment as answer this option is not available to me. :)

Guess we'll just have to leave it like it is.