Render User control as string template - asp.net

what do you think of rendering user control as a string template, basically an implementation like Irenderable implementation do you guys think it has any cons. One of the pros that i came across was that i can use my user controls to simply return the required HTML for ajax calls.

You could also use this approach to render html for emails or PDF. I've done a lot of hackery to grab my pages and render their output to a string builder so I can convert them or email.
I do this when you need a print to pdf for the current page, or email this page and you want to send the html that the user is currently looking at.

Related

ASP.NET login form that works well with browsers 'remember password' feature

I know this has been asked several times, but I'm not happy with the answers so far.
Root problem is the way the browsers detect a "login form" to auto-fill the fields and decide if they should ask the user to save the info. It works fine if your form has just the basic login fields. If the same form has lots of other fields, the browser doesn't detect it as a login form.
The solutions I've seen say to use a separate non-ASPX form on your page. In my case all of my input fields are rather complex subclassed versions of ASPX controls, mostly to get stuff like a custom background image and nice handling of place-holder/watermark text and items of that nature. So trying to create a non-ASPX form is a lot more work since I can't use these controls on the form. I'd have to replicate them in standard HTML. I could do this, but would rather not. Plus it's more maintenance anytime I need to make a change.
Is there a better way? That lets me use all of the greatness that is ASP.NET.
My page has both a login area and a register area (name, email, DOB, etc). This is why the browsers are confused.
I seem to recall when I was writing a website for mobile browsers that there was some markup that could be used to tell the browser "this is a login name field, and this is a password field". If something like that was available, that would be perfect.
Maybe I could do something with an iframe? Like an ASPX page inside an ASPX page?
Despite the complexity of your code, any custom ASP.NET user controls will eventually render output as HTML and every browsers only speak HTML
What you 're looking for is how to write ASP.NET code that results HTML mark-up containing the simple attribute..
<input type="password" autocomplet="off" ...
So that all browser will able to recognize it's a form containing password field. It doesn't care what you do in code behind. Focus on the output.

asp.net obtain copy of rendered HTML

I am creating a website that will offer the option to email the user a copy of dynamic, code-behind, calculation-driven content on a final page of a website. To send an email as of now I obtain the calculation values from the model object again when the user clicks the button, writing all HTML tags by hand, sticking the model data in where needed.
My question is: is there any easier way to copy website output to an html formatted email? I currently code an HTML email by hand and would like the ability to just get a copy of the rendered HTML, and possible modify it from there.
The easiest way (at least for me) is to actually create a webpage that does everything that you want it to do and then scrape it using System.Net.WebClient or System.Net.HttpWebRequest.
So you've got Page1.aspx which allows a person to select some things and submit a form and then you've got Email1.aspx which is the final output. When Page1.aspx is submitted use WebClient to download the contents of Email1.aspx, probably passing a querystring or some cookies over if needed. The user never "sees" Email1.aspx (except in their email of course), its just used behind the scenes.
The advantage of this approach is that you can test the HTML output without having to jump through SMTP hoops. In the Email1.aspx page you could also override the OnRender() method (I think that's the one, might have a different name possibly) if you need to modify the HTML. Or you could modify the HTML after downloading it. If you're performing the same basic HTML modifications you could subclass System.Web.UI.Page, implement your custom rendering and then have all of the emails inherit from your new subclass.
One thing that's important to remember, going down this route creates an HTTP request that's completely separate from the user's request, so things like session and cookies aren't passed over automatically, you'll need to find a way to do that on your own.
You can access the final ready-to-render html using the asp.net
Response.WriteFile("filename");
method

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.

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

How do I retrieve HTML dynamically generated from an aspx page?

To be specific, here is what I am doing, and here is what I am trying to do:
I'm coding an ASP.NET page, with VB code behind. When the user clicks a button on the page, I send them an email with information and instructions. Rather than sending a plain text email, I send a nice, pretty, HTML-formatted one. Right now, I'm doing this in a way that I KNOW will be difficult to maintain. That is, I'm straight up writing out all of the html. i.e.
markup += "<fieldset>"
markup += "<legend>"
markup += "Required Documents"
markup += "</legend>"
...and so on. Is there a way to create an aspx page (with vb code behind), and send the html of that page in the body of the email? The information is dynamic, so this pseudo-page would need logic in the on-load event to format the html correctly.
Thanks!
WebClient client = new WebClient ();
string html = client.DownloadString("http://domain.com/emailtemplate.aspx?id=1");
If you have access to a database you can always drop the html in there otherwise, I solved this problem by creating a mailtemplate.html file with [replace] sections in it so all you have to do is read the file into a string object do your replaces and then send it out. If you have to you can maintain multiple templates this way. I use it mostly as a wrapper on emails my systems need to send out so my template has a [body] tag in it that gets replaced with whatever message I need to send. I have also used this method to wrap multiple files into a single email output.
I assume you want to build the html on the fly... One (certainly the most maintainable) solution is to build a template based system.
Technically you maintain your html (e.g. email shots) in a directory read the templates from your ASP.NET program, fill in the details and send the html mail to the user.

Resources