tags:

views:

570

answers:

3

For an unknown reason, I have 1 page that I can't access by ID to any of the component.

Here is some informations. The page use asp:Content because the website use MasterPage. Inside the asp:Content, this page has a asp:FormView with some data that I cannot access from the CodeBehind.

Here is the Page declaration:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="InfoAccount.aspx.cs" Inherits="Private_InfoAccount" MasterPageFile="~/MasterPagePrincipal.master" %>

Here is the code in the page behind that doesn't compile :

protected void FormView1_PreRender(object sender, EventArgs e)
{
    DateBirthdayValidator.MaximumValue = DateTime.Now.Date.ToString("dd-MM-yy");
}

Here is the error :

Error 2 The name 'DateBirthdayValidator' does not exist in the current context

I have search in google, I got some answer about using FindControl but it doesn't work. Any idea?

Edit1:

I can access the FormView1 component but no the validator inside the EditItemTemplate. How can I access control that is inside the EditTemplate?

Edit2:

If I try: FormView1.FindControl("DateBirthdayValidator") it compile but always return null. So still doesn't work but at least I can access the FormView1...

A: 

The problem is the validator you have declared in the form view does not really exist at the page level. It's a template that you define for your FormView. The parent control can instantiate a template (zero, one or more times, for instance think about each row in a GridView; and as a consequence, create its controls). You should try accessing it like:

// Replace RangeValidator with the actual validator type, if different.
var v = (RangeValidator)myFormView.Row.FindControl("DateBirthdayValidator");
v.MaximumValue = ...;

Note that to be able to do this, your form view should be in the mode you declared your validator in (you can check this with the CurrentMode property) and you should have already called DataBind to bind it to a data source (so that at least single row exists, and thus an instance of the template is created).

Mehrdad Afshari
Ok but I can't access the component in any method (not only _PreRender).
Daok
@Daok: If the issue I said is the problem, it shouldn't work in any method. It's a compile time thing. At compile time, no such field is available for the base class
Mehrdad Afshari
I updated the answer to reflect your update in the question.
Mehrdad Afshari
A: 

Do you have a DateBirthdayValidator declared in the aspx page? Also check to ensure that Visual Studio create a corresponding DateBirthdayValidator declaration in the designer file for your page. It sounds like most likely the designer file is missing the declaration for the control.

Andrew Hare
The ASPX has a DateBirhtdayValidator inside the EditItemTemplate of the FormView. By the way, I can access the FormView in the codebehind!?
Daok
+2  A: 
protected void FormView1_PreRender(object sender, EventArgs e)
{
    if(FormView1.CurrentMode == FormViewMode.Edit)
        ((RangeValidator)FormView1.FindControl("DateBirthdayValidator")).MaximumValue = DateTime.Now.Date.ToString("dd-MM-yy");
}

The FormView doesn't have the control created until it's in the mode you want. Since the DateBirhdayValidator was in the Edit Mode, it required to have a validation in the CodeBehind to be sure to Find the control only when the state is in edit.

I found the solution here, see the post from Steven Cheng[MSFT].

Daok