Html in ASP.NET server control - asp.net

Is there a way to write pure HTML when i'm developing my ServerControl in ASP.NET?
I want to create my control as .dll file. But when I'm writing my control I can only add HTML tags and attributes in C# in my .cs file.
And so i am forced to use this in my RenderContents() method:
output.RenderBeginTag(...
output.AddAttribute(...
output.RenderEndTag(...
instead of for example:
<div attribute1="value"></div>
is there a workaround of this problem?

Unfortunately only user controls (ASCX) allow for this type of approach (delcarative markup) but they cannot be packaged into DLLs. I've always hated this restriction, to me it's a code smell programmatically generating markup, but this is what we must live with!
If you have very static markup you could always put it into a resource file (resx) instead of outputting the HTML programmatically

Related

returning plain html from page method

I would like for a webmethod to return a string of pure html.
The html isnt anything too fancy, a table and a couple form fields populated with values from a database.
Is their an easy way to build html within html? currently I am just dynamically building up a string, but it just feels dirty.
I could make a full blown aspx page, and then just strip out the bits I dont want from the resulting page leaving the plain html behind. but that also feels dirty.
Or there is the templating option (nvelocity etc) but thats seems like overkill.
any suggestion on something more efficient.
Tx
You can build a template file like xml to write the basic of your html with {0}{1}{2}{n} to put static content in it
Manipulate this template server side as you wish. You can add a list of data into this template.
Create a literal control on your page.
Put the server side created html in to you literal control. (myLiteral.Text)
You can create a basic asp.net file and add the html template in it by adding it from the literal control.

How to reference server side controls in JQuery ASP.NET?

What is the recommended approach to reference ASP.NET server side controls in JQuery?Currently I use something a mix of server+client side
$('#<%=txttest.ClientID %>').focus();
I read somewhere it is not a good approach and it slows down things somewhat.
It does not slow things at all from the selector point of view.
it will slow down things because this code will have to be inside the HTML and not inside the JS which can be compressed and minified.
As a .net developer in my past (not that far), I always prefered selectors to refer classes then ID's.
something like
$("input.textInputClassName").val('this is the new value');
because in .net you have no control over the ID's (ARGHHHHH) then you should select using classes (IMHO of course)
$('#<%=txttest.ClientID %>').focus(); this code works only when you have written java Script in the same .aspx page but when your using separate javascript file for your page then this code will not work.
The approach what i prefer is if the controls are static then i will specify the
name
Ex : $('#txttest').focus();
or if the controls were created dynamically then i will register the script or add the attributes to the page
Ex : BtnDailog.Attributes.add("onclick","$("#divMsg.show()");

how to use codebeside in ASP.NET Web Application

I'm using VS2008 and want to create a web application (not a web site) with Code-Beside
but, the default mode of aspx is Code-Behind.
I have tried to change the CodeBehind=ClassFile.cs to CodeFile=ClassFile.cs in the header of aspx's <%#Page%> part, and deleted the aspx.designer.cs file,but if I added a server control to the page, the compiler is also send me an error of no member defined.the cs file is the orinal file of codebehind, it is partial class.
You don't want to delete aspx.designer.cs you want to delete the aspx.cs file, then place a similar file next to it and declare it as a partial class. designer.aspx.cs is still required to provide you direct access to controls placed within the page, rather than going through FindControl.
You definitely don't want to delete the .designer.cs file, as this is where the server control definitions will be placed.
In general the codebehind model is much better as it makes the code easier to find, use and maintain.

Can you use an ASP.NET web control (ASCX) to render XML?

Off the top of my head, I don't see any reason why you couldn't structure an ASP.NET custom web control (ASCX file) to output only XML.
If you avoid using controls in the System.Web.UI namespace, and just use XML tags and sub-controls, which also render only XML, you should end up with a perfectly valid XML document being rendered.
Am I right, or is there something I'm missing?
If you want to generate an XML document and return that to the client, then it's probably easier if you create a http handler (IHttpHandler) instead of using a page and user controls.
You are missing that the control has to be within a page which would have some HTML output for a tag marking the start and finish of the control though there may be a way to override that.
Your idea could work out if your ASCX's generate pure XML, and you overwrite the Page's Render methods to replace the HTML tags that form HTML, BODY and FORM etc. elements.
The question is what you would gain with this approach over simply generating an XML document and building a class library which generates those XML fragments you wanted to generate using the ASCX's.

ASP.NET, XSLT and dynamic controls

I wish to use xml and xsl to generate controls on an asp.net page.
I currently have a asp.net content page that contains a xml control. When the page is loaded, an xml file is loaded and the required element is extracted and set as the xml control's DocumentContent and the xml control's TransformSource is set to the appropriate xsl file.
In the xsl file, I wish to use templates to create asp.net controls depending upon the contents of the xml. Is there an easy way of doing this with the xml control on the content page?
Register a namespace in your XSL so that you can put .NET server-side tags into it. Run your XSL transform against your XML. Run the result of your transform through Page.ParseControls(). This will give you a nice control hierarchy. Add the output of that function to your Controls collection (or the controls collection of your placeholder) and you're good to go.
There are LOTS of caveats to using dynamic controls in .NET, so beware.
The generated output of transform is not parsed to be added to the page as set of controls. Rather the generated output is sent to the response. Therefore you cannot apply a transform to an XML control that will generate new controls.
There may be a way to create the transform result and invoke some parsing that can be applied to the page but that would not meet your 'easy' requirement.
I don't know of any easy way to do it, but there are a few systems out there which generate the aspnet markup on the fly then send it to the aspnet runtime from a memory stream (or similar).
I think umbraco uses a system something like that, but the initial builder mechanism is probably a little more low level than an aspnet control. It might be achievable using an httpmodule or something else that gets hit earlier in the request pipeline.

Resources