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

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

Related

how to use functionality from existing aspx page in usercontrol?

I am trying to use existing functionality which is embedded in a asp.net aspx page. So I have controls on this page and code behind for it. What is the best way to reuse this functionality in a new ascx control? Or do I just have to copy everyting into the control?
Can you not just take the controls you want to use from the aspx page and turn those into a control? Then you can use that control wherever you like.
To access the page parent just written this.Page in your control. But for access to these methods, you must apply an interface to the aspx page.

Writing HTML to a user control

I'm creating a web user control and want to get some html from a datasource and write it to the page. I initially though I should just use response.write, but the problem with that is it writes the message above everything in the page the user control lives in - I guess because the uc is built before the page.
Does any one know of a better approach?
you can use Literal control to write HTML code

Add a usercontrol as the value of a html attribute

I am working in a CMS where we use tokens ( which is turned into a user control. Is there a way to add the user control into an attribute value for our template style?
example :
<div class="<$tokenName/$>" />
this currently outputs an encoded user control, which is then not parsed by IIS.
Short answer: this is not possible.
Longer answer...
It's not IIS's job to parse the control... that happens when IIS hands off the request to the ASP.NET engine. ASP.NET does a single-pass parse through your ASPX before the Page lifecycle even starts... this is why controls you delcare in the ASPX are available during the Init event. Whenever your CMS expands "$tokenName", you are far past the point at which ASP.NET is interpreting your markup.
If you're having trouble with that, here's a thought experiment for you: What happens when $token expands into a user control that has some other $token2 control embedded in it? And that control contains some other $token3? How many times are you going to try and parse/expand/interpret your markup?

controls in code behind

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.

Possible to load ASCX with jQuery.load() function?

Is it possible without encapsulating these controls in aspx files? The Register tag makes me think that it is not.
You can't call ascx page directly, they can't serve any html without added to aspx or render then dynamically in response to any page/handler.
One possible way would be to have a generic aspx stub page that acts as a host to the ascx in the normal web forms way make a callback to the that page using JQuery's $.ajax() method with some arguments to denote what ascx control you want to load.
Then on your stub / host page, override the render method and render the control directly to the output stream using response.output.write. The callback handler on the client will catch the output and then it can be inserted into the DOM in the normal way or using using the version of $.load() that allows the specification of a DOM element.
hope this helps.
This is one way to load ASCX controls through jQuery, using an ASMX service that creates a Page instance to render the user control.
Since I've started using jQuery/Ajax more and more, I find I've been using server controls less and less.
Things that I used to put into controls are now just individual aspx pages which get loaded into panels.
If you have old ASCX files, it shouldn't be too hard to convert them to ASPX
No.
That said, you could place your ascx inside an UpdatePanel and set the panel's content visible/invisible using the standard Asp.Net postback mechanisms.
Or you could write a separate IHttpHandler which generates HTML code that your Javascript code adds to the page using DOM. This part of the page would however not be accessible in PostBack.
No, ASCX are user-controls, that exist on ASPX. You will need to make it a Page, or convert into Handler.

Resources