在代码后面或仅在aspx中声明Web控件

时间:2021-05-24 15:52:55

In .net 1.1 I seem to remember I had to declare web controls that I had put on my aspx page also in the code behind in order to access them programatically. Since working in VS 2008 I haven't had to do that - even when working with .net 2 projects.

在.net 1.1中,我似乎记得我必须在后面的代码中声明我在aspx页面上放置的Web控件,以便以编程方式访问它们。自从在VS 2008工作以来,我没有必要这样做 - 即使在使用.net 2项目时也是如此。

But I just imported a 1.1 project and converted it in 2008 and added a textbox with an id of results to the aspx page and I can't programatically access that from the code behind - I'm getting a compile error.

但是我刚刚导入了一个1.1项目并在2008年转换它并将一个带有结果id的文本框添加到aspx页面,我无法以编程方式从后面的代码中访问它 - 我收到编译错误。

Is this a setting in VS or on the top of the aspx page?

这是VS中的设置还是aspx页面的顶部?

1 个解决方案

#1


Just open the page in the designer, give it a moment to "settle" (maybe no longer necessary, but we used to need to do this with the old designer code), then "touch" the page (change something, then change it back), then save the page, still in the designer. See if that works.

只需在设计器中打开页面,给它一点时间“解决”(可能不再需要,但我们过去需要使用旧的设计器代码执行此操作),然后“触摸”页面(更改内容,然后更改它)然后保存页面,仍然在设计器中。看看是否有效。

It may be necessary to add the text box in the designer.

可能需要在设计器中添加文本框。

Your VS2008 project may be a web site "project", in which case, the controls are not emitted into code by the designer. The VS2003 project is in the style that requires the controls to be in code as well, so use the designer, which was assumed in VS2003.

您的VS2008项目可能是一个网站“项目”,在这种情况下,设计师不会将控件发送到代码中。 VS2003项目的风格要求控件也在代码中,因此请使用VS2003中假设的设计器。

#1


Just open the page in the designer, give it a moment to "settle" (maybe no longer necessary, but we used to need to do this with the old designer code), then "touch" the page (change something, then change it back), then save the page, still in the designer. See if that works.

只需在设计器中打开页面,给它一点时间“解决”(可能不再需要,但我们过去需要使用旧的设计器代码执行此操作),然后“触摸”页面(更改内容,然后更改它)然后保存页面,仍然在设计器中。看看是否有效。

It may be necessary to add the text box in the designer.

可能需要在设计器中添加文本框。

Your VS2008 project may be a web site "project", in which case, the controls are not emitted into code by the designer. The VS2003 project is in the style that requires the controls to be in code as well, so use the designer, which was assumed in VS2003.

您的VS2008项目可能是一个网站“项目”,在这种情况下,设计师不会将控件发送到代码中。 VS2003项目的风格要求控件也在代码中,因此请使用VS2003中假设的设计器。