1

How would I go about finding on my ASPX page (from codebehind) and then adding the attribute "runat=server" to it? I have tried using Page.header.attributes.add(...) and(HtmlHead) Page.FindControl("head"); The second one obviously won't work as the Head tag doesnt have an ID.

I can't work out how to change this property and I can't change or add any additional code to the ASPX page - like ID's etc.

4
  • 1
    The element will not be available in the Controls collection unless it has the runat="server" attribute set in the template or is enclosed in an element with that attribute, so I am not aware of an easy way you can add it within the framework. Commented Apr 27, 2012 at 12:41
  • 3
    You can't add runat="server" from code-behind, because you can't access it programmatically unless it has runat="server" in the first place. Kind of a catch 22. Commented Apr 27, 2012 at 12:42
  • there is a sample here:stackoverflow.com/questions/8600772/… Commented Apr 27, 2012 at 12:43
  • I thought that might be the case... Hmm. Thanks anyway Commented Apr 27, 2012 at 12:43

2 Answers 2

5

Unfortunately, this is not possible.

The runat="server" attribute allows the code behind to "see" the element it decorates. If that attribute is not present in the page markup, the element will not be accessible from code behind and you will not be able to manipulate it.

The only way to achieve what you want is to add runat="server" (and possibly an ID attribute) to the element in the page markup. Only then will you be able to add other attributes, event handlers, etc. from the code behind.

Sign up to request clarification or add additional context in comments.

5 Comments

Well, you can get at it without the ID, the ID just makes it easier, but the runat server is still needed...
@Servy, you're right in theory, but without an ID attribute there will be no member variable representing the element, and FindControl() will not be useable either. The only solution in that case is to identify the element from its position in the control collection, or by some other non-unique attribute, which is less robust.
I thought that this might be the case, I just wanted to avoid editing every single page in the application due to a mass check-in. I'll mark this as an answer when the time is up :)
@FrédéricHamidi That's correct, but it still doesn't change the fact that it will exist in the Controls collection, so you can access it, it's just harder to access it directly.
@Servy, true, and on second thought Page.Header is a special case that requires neither a member variable nor FindControl(), so an ID attribute is definitely not necessary here. I'll update my answer accordingly.
0

The runat directive is a compile-time directive.

You can't add the attribute to existing elements at runtime.

Comments

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.