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.


This content originally appeared on Modern Web Development with Chrome and was authored by Paul Kinlan

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#.<p />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.<p />To do this from C#, if you are rendering out a serverside control you could simply have the following:<p />HtmlGenericControl b = new HtmlGenericControl("b");b.ID="test";......b.Attributes.Add("kinlaniscool", false);this.Page.Controls.Add(b);<p />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"><p />If you wanted to do something similar in a custom server control, I might do the following:<p />protected override void Render(HtmlTextWriter output){output.AddAttribute("kinlaniscool", "false");output.AddAttribute("ID", "test");output.RenderBeginTag(HtmlTextWriterTag.B);output.Write("Yo!");output.RenderEndTag();}<p />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.<p />And that is about it.<p />


This content originally appeared on Modern Web Development with Chrome and was authored by Paul Kinlan


Print Share Comment Cite Upload Translate Updates
APA

Paul Kinlan | Sciencx (2006-04-20T00:00:00+00:00) c# personal attribute to an html tag. Retrieved from https://www.scien.cx/2006/04/20/c-personal-attribute-to-an-html-tag/

MLA
" » c# personal attribute to an html tag." Paul Kinlan | Sciencx - Thursday April 20, 2006, https://www.scien.cx/2006/04/20/c-personal-attribute-to-an-html-tag/
HARVARD
Paul Kinlan | Sciencx Thursday April 20, 2006 » c# personal attribute to an html tag., viewed ,<https://www.scien.cx/2006/04/20/c-personal-attribute-to-an-html-tag/>
VANCOUVER
Paul Kinlan | Sciencx - » c# personal attribute to an html tag. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2006/04/20/c-personal-attribute-to-an-html-tag/
CHICAGO
" » c# personal attribute to an html tag." Paul Kinlan | Sciencx - Accessed . https://www.scien.cx/2006/04/20/c-personal-attribute-to-an-html-tag/
IEEE
" » c# personal attribute to an html tag." Paul Kinlan | Sciencx [Online]. Available: https://www.scien.cx/2006/04/20/c-personal-attribute-to-an-html-tag/. [Accessed: ]
rf:citation
» c# personal attribute to an html tag | Paul Kinlan | Sciencx | https://www.scien.cx/2006/04/20/c-personal-attribute-to-an-html-tag/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.