开发者

controls in code behind

开发者 https://www.devze.com 2022-12-09 10:50 出处:网络
i was wo开发者_开发知识库rking on a vb.net project and i couldn\'t find some controls in the code-behind file(*.vb). i was wondering is it because i was working in page_load function so controls are n

i was wo开发者_开发知识库rking on a vb.net project and i couldn't find some controls in the code-behind file(*.vb). i was wondering is it because i was working in page_load function so controls are not loaded until after page_control event. however i was able to find them with findcontrol function of formview objective.


Controls inside of templates (such as in your FormView, or in a GridView) are not directly accessible in the code behind. You must use FindControl to get access to those controls.


If the controls are declared in the aspx then they're defined in partial class equivalent for your Page class. This was introduced along with .Net v2.0 so that messing with the designer wouldn't screw up with your code behind file (which caused quite a few problems in some cases).

You can access the controls from your Page Load event. Sometimes IntelliSense plays tricks on you and doesn't suggest the control. Just type it in. It will work. You can close the aspx page and open it again. Sometimes that fixes it. Or just restart Visual Studio if you're annoyed by it.

However, there are a few considerations if you are interested in accessing control data at certain times during the life cycle of the page. Server controls have their own life cycle that is similar to the Page life cycle, but the order in which the event is triggered for the controls is as follows:

  1. Init and Unload event for a control occurs before the event is raised for the container (bottom-up).
  2. Load event for a control occurs after the event is raised for the container (top-down).

You can find a more detailed explanation of the Page life cycle events on MSDN.


It's hard to tell what exactly the problem is; it would help if you could post some code here.

I do have two guesses/suggestions:

  1. If you have the problem that brentkeller is describing, what usually fixes this completely for me is deleting the aspx.designer.cs file, then right-clicking on the .aspx file and select "Convert to Web Application". This re-creates the designer file.

  2. The control is inside a template like Jason Berkan suggested. If it's in a LoginView, for example, you would use .FindControl("controlId") on the LoginView.


The controls would be part of a partial class in the same solution. Just find all references for your class name.


I occasionally have trouble with adding a control to a page and the Intellisense not recognizing the control. The compiler also seems to not recognize the control and prevents the project from being compiled. It can be very frustrating and I really haven't figured out why.

Sometimes it helps to close the aspx page and its code file, sometimes closing Visual Studio and re-opening it works. Sometimes none of it works and I just try another way to get things done.

I don't know if this is what you're experiencing, but if so, it can definitely make you scratch your head and wonder what is going on.

0

精彩评论

暂无评论...
验证码 换一张
取 消