ASP.Net codebehind无法从页面访问组件?

时间:2023-01-08 08:09:43

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

由于未知原因,我有1页我无法通过ID访问任何组件。

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.

这是一些信息。页面使用asp:Content因为网站使用MasterPage。在asp:Content中,这个页面有一个asp:FormView,其中包含一些我无法从CodeBehind访问的数据。

Here is the Page declaration:

这是Page声明:

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

错误2当前上下文中不存在名称“DateBirthdayValidator”

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

我在谷歌搜索,我得到一些关于使用FindControl的答案,但它不起作用。任何的想法?

Edit1:

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

我可以访问FormView1组件,但没有EditItemTemplate中的验证器。如何访问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...

如果我尝试:FormView1.FindControl(“DateBirthdayValidator”)它编译但始终返回null。所以仍然无法工作,但至少我可以访问FormView1 ...

3 个解决方案

#1


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.

FormView没有创建控件,直到它处于您想要的模式。由于DateBirhdayValidator处于编辑模式,因此需要在CodeBehind中进行验证,以确保仅在状态处于编辑状态时才查找控件。

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

我在这里找到了解决方案,请参阅Steven Cheng [MSFT]的帖子。

#2


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:

问题是您在窗体视图中声明的验证器在页面级别并不存在。它是您为FormView定义的模板。父控件可以实例化一个模板(零,一次或多次,例如考虑GridView中的每一行;因此,创建其控件)。您应该尝试访问它,如:

// 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).

请注意,为了能够执行此操作,您的表单视图应该处于您声明验证器的模式中(您可以使用CurrentMode属性进行检查),并且您应该已经调用DataBind将其绑定到数据源(以便在存在最少的单行,因此创建模板的实例)。

#3


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.

您是否在aspx页面中声明了DateBirthdayValidator?还要检查以确保Visual Studio在页面的设计器文件中创建相应的DateBirthdayValidator声明。听起来很可能设计器文件缺少控件的声明。

#1


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.

FormView没有创建控件,直到它处于您想要的模式。由于DateBirhdayValidator处于编辑模式,因此需要在CodeBehind中进行验证,以确保仅在状态处于编辑状态时才查找控件。

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

我在这里找到了解决方案,请参阅Steven Cheng [MSFT]的帖子。

#2


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:

问题是您在窗体视图中声明的验证器在页面级别并不存在。它是您为FormView定义的模板。父控件可以实例化一个模板(零,一次或多次,例如考虑GridView中的每一行;因此,创建其控件)。您应该尝试访问它,如:

// 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).

请注意,为了能够执行此操作,您的表单视图应该处于您声明验证器的模式中(您可以使用CurrentMode属性进行检查),并且您应该已经调用DataBind将其绑定到数据源(以便在存在最少的单行,因此创建模板的实例)。

#3


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.

您是否在aspx页面中声明了DateBirthdayValidator?还要检查以确保Visual Studio在页面的设计器文件中创建相应的DateBirthdayValidator声明。听起来很可能设计器文件缺少控件的声明。