Solved

How do I access validation controls programmatically in GridView EditItemTemplate?

Posted on 2009-05-11
14
1,044 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 9

Author Comment

by:Ken Fayal
Comment Utility
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
Comment Utility
Did you try hooking into the RowCreated Event?
0
 
LVL 9

Author Comment

by:Ken Fayal
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 9

Expert Comment

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

Author Closing Comment

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

Expert Comment

by:tillgeffken
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now