Solved

How do I access validation controls programmatically in GridView EditItemTemplate?

Posted on 2009-05-11
14
1,049 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:Ken Fayal
  • 7
  • 7
14 Comments
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24352331
You could add your validator programmatically in the RowDataBound event.

http://www.aspdotnetcodes.com/GridView_Dynamic_Validation.aspx
0
 
LVL 9

Author Comment

by:Ken Fayal
ID: 24352421
I have considered doing this but I'd like to be able to see if I can touch the declared validators first.
0
 
LVL 9

Accepted Solution

by:
tillgeffken earned 500 total points
ID: 24352483
Well, it should be accessible in the RowDataBound event too. However i'm unsure about FindControlRecursive.

Never had problems with something like:

void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
  if(e.Row.RowType == DataControlRowType.DataRow)
  {
    RequiredFieldValidator rfv = (RequiredFieldValidator)e.Row.Cells[0].FindControl("reqFldVal4");
    rfv.ErrorMessage = Localization.GetString("DepositDate_Req_Validation.ErrorMessage", LocalResourceFile);
  }
}

Open in new window

0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 9

Author Comment

by:Ken Fayal
ID: 24361804
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.
0
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24361880
Did you try hooking into the RowCreated Event?
0
 
LVL 9

Author Comment

by:Ken Fayal
ID: 24362234
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.  
0
 
LVL 9

Author Comment

by:Ken Fayal
ID: 24372734
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

0
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24372780
But it makes perfect sense. Glad you worked it out.
0
 
LVL 9

Author Closing Comment

by:Ken Fayal
ID: 31580050
Giving you the points since you stuck with me on it.
0
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24373283
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.
0
 
LVL 9

Author Comment

by:Ken Fayal
ID: 24373306
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.
0
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24373333
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.
0
 
LVL 9

Author Comment

by:Ken Fayal
ID: 24379026
I'm told that the question is now closed and the option to accept the solution has been removed.
0
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24379383
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question