简体   繁体   English

ASP.Net代码背后无法从页面访问组件?

[英]ASP.Net codebehind can't access component from page?

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. 该页面使用asp:Content,因为该网站使用MasterPage。 Inside the asp:Content, this page has a asp:FormView with some data that I cannot access from the CodeBehind. 在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. 我在google中搜索,我得到了一些有关使用FindControl的答案,但是它不起作用。 Any idea? 任何想法?

Edit1: 编辑1:

I can access the FormView1 component but no the validator inside the EditItemTemplate. 我可以访问FormView1组件,但不能访问EditItemTemplate内部的验证器。 How can I access control that is inside the EditTemplate? 如何访问EditTemplate内部的控件?

Edit2: 编辑2:

If I try: FormView1.FindControl("DateBirthdayValidator") it compile but always return null. 如果尝试: FormView1.FindControl("DateBirthdayValidator")可以编译,但始终返回null。 So still doesn't work but at least I can access the FormView1... 因此仍然无法正常工作,但至少我可以访问FormView1 ...

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. 在FormView处于所需模式之前,它不会创建控件。 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. 由于DateBirhdayValidator处于“编辑”模式,因此它需要在CodeBehind中进行验证,以确保仅在状态处于编辑状态时才能找到该控件。

I found the solution here , see the post from Steven Cheng[MSFT]. 我在这里找到了解决方案,请参阅Steven Cheng [MSFT]的帖子。

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 . 这是您为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). 父控件可以实例化模板(零次,一次或多次,例如考虑GridView每一行;并因此创建其控件)。 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). 请注意,要执行此操作,您的表单视图应该处于您在其中声明验证器的模式(可以使用CurrentMode属性进行检查),并且您应该已经调用了DataBind将其绑定到数据源(以便在至少存在一行,因此创建了模板的实例)。

Do you have a DateBirthdayValidator declared in the aspx page? 您是否在aspx页面中声明了DateBirthdayValidator Also check to ensure that Visual Studio create a corresponding DateBirthdayValidator declaration in the designer file for your page. 还要检查以确保Visual Studio在页面的设计器文件中创建了相应的DateBirthdayValidator声明。 It sounds like most likely the designer file is missing the declaration for the control. 听起来设计器文件很可能缺少控件的声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM