. Topicala Page Index Token

A Journal about the experiences I have developing little applications in C#, Perl, Html and Javascript and talking about things new things that I use. Always Geeky; Always Nerdy; Always poor Grammer!

I am a Software Analyst Developer working in Southport, England but living in Liverpool. I develop mainly in C# and ASP.Net. I have been developing comercial software for several years now. I maintain this site (hosted at SwitchMedia UK) as a way of exploring new technologies (such as AJAX) and just generally talking about techie geek issues. This site is developed through a host of Perl scripts and a liberal use of Javascript. I enjoy experimenting with new technologies and anything that I make I host here.

Quick Search

Web www.kinlan.co.uk

Thursday, April 20, 2006

c# personal attribute to an html tag

A Visitor to my site came from google looking for "c# personal attribute to an html tag". If I understand this query correctly then the person is trying to add an attribute to an already created HTML object. However, this might not be the case, so I will include in this entry a few different ways of adding attibutes to HTML elements via C#.

Firstly, I am presuming that they are using IE to view the HTML. IE is pretty cool about attributes being added to HTML elements. They treat these attributes on HTML like expando objects (you can add them dynamically in Javascript too). So an element <b id="testB"> can have an attribute kinlaniscool simply by using the following Javascript: document.all.testB.kinlaniscool="false"; It could have been defined in the HTML as <b id="testB" kinlaniscool="false"> and you would get the same results.

To do this from C#, if you are rendering out a serverside control you could simply have the following:

HtmlGenericControl b = new HtmlGenericControl("b");
b.Attributes.Add("kinlaniscool", false);

The above code might add a control to the page which is a "b" tag and that will look like: <b id="testB" kinlaniscool="false">

If you wanted to do something similar in a custom server control, I might do the following:

protected override void Render(HtmlTextWriter output)
output.AddAttribute("kinlaniscool", "false");
output.AddAttribute("ID", "test");

Basically all we are doing in the above code is overriding how the control renders its data. It will render a B tag with all the correct attributes.

And that is about it.

Related Tags
[feed], [feed], [feed], [feed], [feed], [feed], [feed], [feed], [feed], [feed]

Related Wikipedia Documents
, , , , , , , , , ,

My Related Documents
, , , ,

Related Amazon Books
Applied .Net Attributes: / Head First Design Patterns: / Head Rush Ajax: / JavaScript: The Definitive Guide: / Programming ASP.NET 2.0 Core Reference: / Programming C#: / ASP.NET 2 for Dummies (For Dummies S.): / C#: /

Related Images From Flickr

Comments: [Add New]