Controls in code

I was working on a vb.net project and I could not find some controls in the code file (*. Vb). I was wondering because I was working in the page_load function, so the controls are not loaded until the page_control event occurs. however I was able to find them with the findcontrol function of the formview target.

+2


source to share


5 answers


Controls inside templates (for example, in a FormView or in a GridView) are not directly accessible in code. You must use FindControl to access these controls.



+2


source


If the controls are declared in aspx, then they are defined in the partial class equivalent for your class. This was introduced with .Net v2.0, so fiddling with the designer wouldn't mess up your code by file (which caused quite a few problems in some cases).

You can access the controls from the Page Load event. Sometimes IntelliSense plays tricks on you and doesn't offer control. Just enter it. It will work. You can close the aspx page and reopen it. Sometimes it fixes it. Or just restart Visual Studio if it annoys you.

However, there are several considerations if you are interested in accessing control data at specific times during the page's life cycle. Server controls have their own life cycle, which is similar to the page life cycle, but the order in which the event is fired for controls is as follows:



  • Init

    and the Unload

    event for the control is set to before , the event is raised for the container (bottom to top).
  • Load

    The event for the control occurs after the event is raised for the container (top to bottom).

More detailed description of page lifecycle events on MSDN .

+1


source


It is difficult to say exactly what the problem is; it would help if you could post the code here.

I have two guesses / suggestions:

  • If you have the problem that brentkeller describes, what usually fixes this completely for me is deleting the aspx.designer.cs file, then right clicking on the .aspx file and selecting "Convert to Web Application". This recreates constructor file.

  • The control is inside the template as Jason Berkan suggested. For example, if in LoginView you use .FindControl ("controlId") in LoginView.

+1


source


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

0


source


I sometimes have problems adding a control to a page and Intellisense does not recognize the control. The compiler also doesn't seem to recognize the control and doesn't allow the project to be compiled. This can be very frustrating and I really didn't understand why.

Sometimes it helps to close the aspx page and its code file, sometimes closing Visual Studio and reopening it. Sometimes none of them work and I'm just trying another way.

I don't know if this is what you are going through, but if it is, it might make you scratch your head and wonder what is going on.

0


source







All Articles