controls in code behind - asp.net

i was working 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:
Init and Unload event for a control occurs before the event is raised for the container (bottom-up).
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:
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.
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.

Related

I get a NullReferenceException when I use FindControl to find controls in either master or content pages

I am trying to create reversible themes in ASP.NET. I can successfully change themes using a dropdown list, but I am running into problems changing SkinID's and generic HTML controls (which are all div's except for the body tag) programmatically. I moved all my attribute- and skin-changing code to the PreInit method of my Base Page. Now I get a NullReferenceException when I run the page. I thought this code was supposed to use the existing controls it is supposedly pointing to. What am I missing or doing wrong?
Here is my code:
The PreInit event is probably too early in the page lifecycle to look for controls with FindControl. You're also doing your declarations outside of the event. That may have something to do with your null reference as well. I'd see if you can change it to look later in the lifecycle. This may be helpful if you haven't already seen it: http://msdn.microsoft.com/en-us/library/ms178472.aspx

.aspx works but .ascx does not... why and how to fix?

I want to put a ASTreeView web control in a custom web control, ASTreeView sample code is like:
<ct:ASTreeView ID="astvMyTree"
runat="server"
...
LoadNodesProvider="~/ASTreeViewDemo5.aspx"
.../>
LoadNodesProvider is the page ajax called when loading a node...however if I changed the provider to my .ascx file, it does not work:
LoadNodesProvider="~/ASTreeViewDemo5.ascx"
it did not even go through the Page_Load part of the .ascx file
Though this might be related with astreeview itself, I'm wondering what the problem could be? anything I can do to fix it?
Thanks!
It is because ascx must have a container ie Page. You can't use it same way as Page.
ASPX is a page and ASCX is a usercontrol. You cannot ajax call a control, so you probably want it to be a page with the control on it.
While it isnt entirely clear to me what LoadNodesProvider is supposed to do, if you want to encapsulate some code or run a process via AJAX you have a couple of options. One would be to create a web service (you could use WCF for this) that the AJAX method could call. Another option would be to create an http handler (ASHX extension typically denotes this). Using an ASPX or ASCX for this doesn't make a whole lot of sense to me. Proco and Tomas are correct regarding the ASCX file, these are Usercontrols and are not stand-alone objects.
If you really, really want to use an ASPX page/ASCX control, then I suppose it would be best to create a blank ASPX page that has one placeholder, and then attach your user control (based on query string parameters or something I guess) to the placeholder to render out the content for your AJAX control

Why does my DataGrid flat refuse to reload itself from ViewState on Postback?

I have a DataGrid control that refuses to reload itself from View State on Postback. I've traced this all the way back to the Load event on the control. It just flat refuses to reload from ViewState. It just doesn't do it.
The control is situated like this:
Webform
--> Dynamically loaded User Control (via "LoadControl")
----> Statically placed User Control
------> DataGrid
Yes, I know it's a little convoluted, but all the other controls on the page are reloading themselves, except for this DataGrid.
I've tried all the usual suspects --
The Control Tree is the same on page load and postback.
I've tried setting TrackViewState as well. That didn't do anything either.
Now, this is the weird part: this worked fine, then broke. I've gone back through Subversion and reviewed all the code changes for the last month, and there's nothing that would have changed this.
This makes me wonder if this can behavior somehow be affected from some external variable -- something external to the code. Is there some setting or something that might change how this behaves?
Are you hitting the issue where the control isn't actually present when its doing the load from ViewState? http://msdn.microsoft.com/en-us/library/ms178472.aspx

ASP.NET: How to initialize when *user control* is initially loaded

I have an ASP.NET user control that I'm embedding in another user control. This works fine.
I need to know the best logic/method for detecting when the control is loaded. In other words, I have some display initialization logic that needs to run when the control is initially displayed. Surely there is a pattern for this.
The typical method is to put (!IsPostBack) logic in the Page_Load method of the control. This works great until you end up with a state when the Parent page has already posted back many times. My user control gets added to the page but its display does not intialize properly.
I'm hoping to find a way that keeps this logic inside the control, versus various hacking around in the codebehind of the parent page.
See the following MS article. They have an example that places several controls within a user control and initializes them.
There is another post here on StackOverflow that seems similar. You may want to check it out, and see if it points you in the right direction.
It may also be helpful to review the page life-cycle and events.

RegisterClientScriptResource + AJAX update panel

I have a problem that is really making me feel dumb. I have a custom control that inherits textbox. This textbox (at least for this example) simply has a .js file that is embedded in the .dll.
Long story short, works great when not in an AJAX update panel. So i did some research and found that instead of using: Page.ClientScript.RegisterClientScriptResource, i should use ScriptManger.RegisterClientScriptResource - i have done this in the overrdive render method, but the javascript still does not fire.
Anyone know what im doing wrong?
example:
i have a folder in my project called Scripts - it contains myscript.js
My Assembly is called Jim.Bob.Controls
I add attributes to my controls ie: Attributes.Add("onclick", "Test2();");
In the Override Render:
ScriptManager.RegisterClientScriptResource(this.Page, typeof(CustomTextBox), "Jim.Bob.Controls.Scripts.myscript.js");
Yet i still get 'object expected' error.
I need my textbox to work with and w/o AJAX. I imported System.Web.Extensions into my project to access ScriptManger
Can someone please tell me what im doing wrong? Again, this whole thing worked fine w/o AJAX, i have put the necessary stuff into the AssemblyInfo.cs (WebResource:,,,,etc)
Thanks in advance :-)
--- UPDATE ---
I reverted the control, trying it in a non AJAX web and i am having the same problem. Not sure why i have a problem, i have another custom control in the same assembly that is working just fine - have them setup the exact same way, only difference is the one that is working inherits WebControl, the one that is not inherits TextBox
...
in the one that is working i emmit html like Go and do it
Where the one that is NOT working i have
Attributes.Add("onclick", "CustomFunction();");
Also, if i do Attributes.Add("onclick", "alert('hello');");
it works fine.
Sorry for such a long post.
Try to pass this instead of this.Page. The ScriptManager would output scripts only for controls which are being updated (children of UpdatePanel that is).
I've just noticed that you are doing this during Render. That's too late. Try PreRender instead.

Resources