Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

How do I access validation controls programmatically in GridView EditItemTemplate?

Avatar of Ken Fayal
Ken FayalFlag for United States of America asked on
14 Comments1 Solution1105 ViewsLast Modified:
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
    <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>
// 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.
Avatar of tillgeffken

Our community of experts have been thoroughly vetted for their expertise and industry experience.

This problem has been solved!
Unlock 1 Answer and 14 Comments.
See Answers