4

I need to add some attributes (http://www.w3.org/International/questions/qa-http-and-lang) to the tag in an ASP.NET Page object. Note: I cannot do this in a declarative manner and have to use the server side object model to do it. Any ideas?

To add some additional information: I need to do this within the ASP.NET Page rendering life cycle. I need to add the attribute to the root element in the page.

2 Answers 2

5

You should see the following MSDN article:

It looks simple enough:

myButton.Attributes.Add("myattribute", "myValue");

Update: You can do this to any element that has an id and is set with runat="server", for example:

<html id="htmlTag" runat="server" ...

this.htmlTag.Attributes.Add("myAttribute", "myValue");
Sign up to request clarification or add additional context in comments.

6 Comments

This is along the lines of what I am looking for except I need to add it to the <html> (root) tag, which I cant see exposed as a control (unless I am missing something)
@Slappy add runat="server" to the HTML tag and give it an id - that should do the trick.
@Kragen is correct with the previous comment. I do this all the time to set the class for our designers. It should apply to the HTML tag too.
Sadly I dont have access to the declarative. But thanks this is the closet answer and will probably work for someone else
@Slappy In that case you might want to try overriding the Render method of the page and using something like the HTML agility pack to directly intercept and modify the output html. See get HTML of current page without ViewState ASP.Net for a kind-of example. Its not really within the ASP.Net page lifecycle, but I'm not aware of many other ways of doing this.
|
0

I think the HTML Agility Pack will give you what you're looking for.

2 Comments

I don't think this will work as I am still within the rendering process and have no hooks to the raw html output.
Gotcha. If you're still within your rendering process then I'd use Kragen's method of adding the runat="server" to your HTML tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.