?
Solved

Why do I get runtime error "Control ... cannot be verified"?

Posted on 2007-08-09
5
Medium Priority
?
856 Views
Last Modified: 2013-11-05
I'm trying to validate a radio button using a asp:CustomValidator. The general idea is that if the button is checked, a text box must have a valid date string. I get this run-time error when the Page.IsValid statement is evaluated -

"System.Web.HttpException: Control 'SettledBtn' referenced by the ControlToValidate property of 'SettledBtnCustomValidator' cannot be validated"

The page uses a Master page, and is organized as follows:

<asp:Content ...
<asp:ScriptManager ...
<asp:Panel
<asp:UpdatePanel
<ContentTemplate>
<table> ...
    <td style="background-color: #F0F5FF">

      <asp:RadioButton ID="SettledBtn" runat="server" GroupName="BatchBtns" Text="Settled"
               CausesValidation="true" OnCheckedChanged="RadioButton_CheckedChanged"  AutoPostBack="True" />

   </td>  ....
    <td style="width: 187px">
    <asp:TextBox ID="FromDate0" runat="server"></asp:TextBox>

    <asp:CustomValidator ID="SettledBtnCustomValidator" runat="server" ErrorMessage="From date required"
    ControlToValidate="SettledBtn" ValidationGroup="DateValidators"
     OnServerValidate="SettledBtnValidator_ServerValidate"  ></asp:CustomValidator>  
...
<\table>
 </ContentTemplate>
 <Triggers>
  <asp:AsyncPostBackTrigger ControlID="FromDate0" EventName="TextChanged" />
  <asp:AsyncPostBackTrigger ControlID="ToDate0" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
</asp:Panel>

There are additional range and compare validator controls attached to the TromDate0 text box.
The code-behind is:

protected void SettledBtnValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
// 'Settled batches' option was selected, so a 'From' date is required
args.IsValid = (SettledBtn.Checked && ! String.IsNullOrEmpty(FromDate0.Text));
}

This is quite frustrating, as I really need a clue. Any ideas why a radio button '... cannot be validated'?
Thanks for your help
0
Comment
Question by:shellback
  • 2
4 Comments
 
LVL 8

Expert Comment

by:sabeesh
ID: 19663535
Here ControlToValidate="FromDate0" text box not button
0
 

Author Comment

by:shellback
ID: 19663636
Thanks sabeesh - however, I just tried changing ControlToValidate to FromDate0. Compiled and published the website. After checking the settled button and leaving the FromDate0 textbox empty, I submitted the page. The desired error message 'From date required' did not appear, and the page continued on.

I misstated when the runtime error occurs - it happens immediately after I check the settled button.
0
 

Author Comment

by:shellback
ID: 19666306
To anyone struggling with a similar problem - it seems that some controls cannot be validated with current validation controls, and radiobutton is one of them, the checkbox is another. I found a work-around suggested in the following discussion of CustomValidator controls (see http://msdn2.microsoft.com/en-us/library/9eee01cx(VS.80).aspx)

Basically, the idea is to use the CustomValidator control without setting the ControlToValidate property. The server-side event handler still executes, however, the Value property of the arguments parameter passed in to the event handler contains an empty string. In my case, this was not a problem, as I could still refer to the controls I wanted to validate.

The solution to the posted problem above:

 <asp:CustomValidator ID="SettledBtnCustomValidator" runat="server" ErrorMessage="From date required"
  ValidationGroup="DateValidators"
 OnServerValidate="SettledBtnValidator_ServerValidate"  ></asp:CustomValidator>  

Note - no ControlToValidate property.  the event handler remains the same:

protected void SettledBtnValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
// 'Settled batches' option was selected, so a 'From' date is required
args.IsValid = (SettledBtn.Checked && !String.IsNullOrEmpty(FromDate0.Text));
}

This works because I don't need to refer to args.Value in the event handler.

Please close this question

0
 
LVL 1

Accepted Solution

by:
Vee_Mod earned 0 total points
ID: 19734861
Closed, 250 points refunded.
Vee_Mod
Community Support Moderator
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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.
Integration Management Part 2
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month16 days, 1 hour left to enroll

850 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