Writing HTML to a user control - asp.net

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

Related

.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

How can I test a User Control without adding to an ASPX file?

Currently if I want to test a user control, I add my custom tags into an existing .aspx page (or create a new one if I have to), and then view this page in the browser.
Is there a quicker, better way to test User Controls which I'm not aware of?
You cannot run a user control like you would a page. You could create a simple test form for testing it...

aspx ashx mash-up

I'm retro-fitting a .aspx page with AJAX functionality (using VB, not C#). The codebehind populates the page with data pulled from a web-service. The page has two panels that are populted (with different data, of course) in this way. On a full page refresh, one or both panels might need to be populated. But populating Panel 2 can take a long time, and I need to be able to update panel 1 without refreshing Panel 2. Hence the need for AJAX (right?)
The solution I've come up with still has the old .aspx page with .aspx.vb codebehind, but introduces a Generic Handler (.ashx) page into the mix. Those first two components do the work on the user's first visit or on a full page refresh, but when AJAX is invoked, the request is handled by the .ashx page.
First question: Is this sound architecture? I haven't found a situation online quite like mine. Originally, I wanted to make the .aspx page into the AJAX handler by having the codebehind implement IHttpRequest, and then providing "ProcessRequest" and "IsReusable" methods, but I found I couldn't separate a regular visit to the page from an AJAX request, so my AJAX handlers took over even on the first visit to the page. Second question: Am I right to think that this approach (making the .aspx page do double-duty as the AJAX handler) will never work? Is it impossible to tell whether we're getting a full-page request or a partial-page (AJAX) request?
If the architecture is good, then I need to dynamically generate a lot of HTML in the .ashx file, right? If that is right, should I send HTML back to the client, or should I encode it in some way? I've heard of JSON encryption, but haven't figured out how to use it yet. So, Third question: Is "context.Response.Write" the only pipeline for sending data back to the client? And, if so, should I send back HTML or some kind of JSON-encoded objects?
Thanks in advance.
It sounds as if the page requires some AJAX functionality added to the UI.
Suggest using an UpdatePanel for each web form element that needs to have AJAXy refresh
functionality. That'll save you from having to refactor a bunch of code, and introduce a whole lot of HTML creation on your .ashx.
It'll be more maintainable over the long run, and require a shorter development cycle.
As pointed out by others, UpdatePanel would be a easier way - but you need to use multiple update panels with UpdateMode property set as conditional. Then you can trigger the update-panel refresh using any button on the page (see AsyncPostBackTrigger) or even using java-script (see this & this). On the server side, you may decide what has triggered the partial post-back and act accordingly by bypassing certain code if not needed.
You can also go with your approach - trick here is to capture the page output using HttpServerUtility.Execute in your ashx and write it back into the response (see this article where this trick has been used to capture user control output). Only limitation with this approach is that you can only simulate GET requests to your page and so you may have to change your page to accept parameters via query string. Personally, I will suggest that you create a user control that accept parameters via method/properties and will generate necessary output and then use the control on your page and in ashx (by dynmaically loading it in a temperory page - see this article).
EDIT: I am using jquery to illustrate how to do it from grid-row-view.
$(document).ready(function() {
$("tr.ajax-grid-row").click(function() {
$("#hidden-field-id").val($(this).find(".row-id").val()); // fill hidden filed
$("#hidden-button-id").click(); // simulate button click
});
});
You can place above script in the head element in markup - it is assuming that you have decorated each grid-row-view with css class "ajax-grid-row" and each row will have hidden field decorated with css class "row-id" to store row identifier or the value that you want to pass to server for that row. You can also use cell (but then you need to use innerHTML to get the value per row). "hidden-field-id" and "hidden-button-id" are client ids for hidden field and submit button - you should use Control.ClientID to get actual control ids if those are server controls.
JSON is not for that purpose, it is to pass objects serialized with a nice light weight notation, is you need to stream dinamically generated html using ashx, response.Write is what you have. You may want to take a look at MVC
Or you could use jquery if it's just html, the simpliest would be the load function, or you can look into Ajax with jquery. Since the ashx can be served as any resource it can be used in the load function.
I agree with #p.campbell and #R0MANARMY here. UpdatePanel could be the easiest approach here.
But then like me, if you don't want to go the UpdatePanel route, I don't see anything wrong with your approach. However, generating the html dynamically (entirely) at the back end is not a route I'll personally prefer (for the maintainence reasons). I'd rather prefer implementing a solution that will keep the design separate from the data.

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?

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