Possible to load ASCX with jQuery.load() function? - asp.net

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.

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.

Rendering .Net controls through Handler for Ajax

I have an ASP.Net application that has a UserControl that needs to be rendered on an ASPX page (as normal), but also via Ajax using a Handler.
One solution to this is to get the UserControl to simply build up a HTML string, rather than rendering controls as it would normally.
That way, the ASPX page can simply populate a Literal control with the output, while the Handler can write the output to the Response object for Ajax to pick up.
Is this the ideal solution? are there any drawbacks of this approach?
If you do that, you have to recreate the user control every time from the client-side when a postback to the server happens. However, performance wise, that may be better. However, I don't think the UC can then participate with viewstate and the ASP.NET object model as a typical user control would. You would have to account that all actions within the UC are handled appropriately, to react to the web.
If you have a large amount of content to render, the UC approach is just fine. We're talking rendering markup and injecting it into the UI, and so that does pan out and can make it easier. If it's not that much markup, any alternative would do.
If all you need to do is push data to the client, an alternative could be that you push JSON to the client through a web service, and build the UI on the client. That's efficient too.
HTH.
There are a number of JS templating tools available now that will solve this problem. Take a look at mustache js, which can allow the same template to be rendered in client side JS and in C#.Net.
You can then create on template file and either render the HTML in JS via Ajax or render in .Net and simply output during page load.

.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

Changing the view (.aspx) from the code-behind (.aspx.cs) class

I have a 'classic' ASP.NET app (.NET 3.5) with a pretty standard runat="server" style form with server-side controls and an 'Execute' asp:button. In the code-behind, the executeButton_click handler processes some of the other controls, runs a report, and drops the result into an asp:label. So the effect of pressing the Execute button is that the entire page reloads with the report inserted into the label.
I need to change this so that when the Execute button is pressed, the report pops up in a new window with a different layout (ie without the controls and form). If I was doing this in an MVC framework I would just change the view template in the 'execute' action, so the analogue for ASP.NET would be change the code in front, ie the .aspx file that gets used, from the code-behind class.
Is this possible? I know the link between the .aspx and the .aspx.cs isn't extremely tight as it is possible to reuse a code-behind class in multiple .aspx files. So can I set the .aspx file to render from the code-behind?
Normally you would direct the user to the new page by using
Response.Redirect("UrlToNewPage.aspx");
However if you need to access properties of the controls that are on the original form there is another approach you can take. Change the "PostBackURL" property of the asp:button to the second page. Then on the second page set the (#Page PreviousPageType) directive at the top of the aspx file, this tells this page that it is intended as the target of a post from page1.aspx. Then, in the code behind of the second page you can access the controls on the first page using the Page.PreviousPage property
For code sharing purposes you can create a base class and have both aspx.cs classes inherit from this base class if you want shared code.
I think this isn't possible because of the way ASP.NET works. The view file in ASP.NET is dynamically compiled into a subclass of the code-behind class by the ASP.NET engine. When the URL is requested, the subclass is what is instantiated and executed. Changing the view from the code-behind class is trying to change the concrete type. We can have multiple .aspx files inheriting from a single code-behind class, but not vice versa.
Non-ASP.NET frameworks would be able to do this because the view is just a template file that is processed on the fly by the framework after the controller action is complete, so the view can be changed without bothering with inheritance issues.

Getting server side javascript over client side

I have created custom control using asp.net 2.0. The control contains a textbox txtDate. I have also created a javascript file DateMask.js which contains a function maskDate(). I have attached the maskDate() with textbox using -
txtDate.Attributes.Add("onkeypress","maskDate()");
I have also registered the script using ClientScript.RegisterStartupScript.
When I execute the aspx page containing my custom control it is generating script error showing that maskDate() is undefined.
Could anybody tell me what exactly the problem is?
Thanks for your cooperation.
One way to do it would be to place a literal control above your textbox, and assign the script to it in the code behind:
literal1.Text = "<script>function maskDate() {...}</script>";
The benefit to this, is you would not need to have to reference the script file with some tricky relative paths depending on where your usercontrol is used.
Make sure you didn't forget <form runat="server" ID="Form1"></form> at the end of the <head> tag!
As you can read in Using JavaScript Along with ASP.NET 2.0 under "The Difference Between Page.ClientScript.RegisterStartupScript and Page.ClientScript.RegisterClientScriptBlock" they rely on the location of the form tag.
We have shown two different methods
for placing JavaScript functions on an
ASP.NET pageā€”so what is the
difference? The main difference is
that the RegisterStartupScript method
places the JavaScript at the bottom of
the ASP.NET page right before the
closing element. The
RegisterClientScriptBlock method
places the JavaScript directly after
the opening element in the
page. So what difference does this
make? It can make quite a bit of
difference, as we will see.

Resources