Hot Licks
Hot Licks

Reputation: 47759

How to set style on a <div> and/or insert a <style> in an aspx file via C#?

OK, up front I need to say that I've been coding C# and .NET off and on since Wednesday a week ago. Just trying to figure out enough to fix some scripts provided by a 3rd party. So I'm flying blind here.

I've got a .aspx file (ie, prototype HTML) that contains some tags that need to be dynamically modified from the Page_Load method in the corresponding .cs file. I'd like to do the modification with minimal tear-up of the files involved.

The tags I need to modify are asp:Label, asp:ImageButton, and div. The first two I think I know how to modify, by referring to the IDs in the .cs and setting the associated attributes. But I can't find an "asp:" equivalent of div, nor any obvious way to modify it without that "asp:" equivalent. I could alternatively use a <style>, but I can't find any way to generate that into the HTML stream (though I see hints here and there). I have the Evjen/Hanselman/Rader ASP.NET book, but though it contains some stuff that seems headed in the right direction, that stuff uses HtmlTextWriter which doesn't seem to be the right interface for use in Page_Load.

So, any clues? I need primarily need to modify the style property inside the div.

Upvotes: 1

Views: 386

Answers (1)

Doozer Blake
Doozer Blake

Reputation: 7797

You can just create an HtmlGenericControl by using

<div runat="server" id="something"></div>

in your code-behind

something.Attributes["style"] = "style it up";

The ASP.NET control equivalent would be an <asp:panel>, but depending on your version, panel used to (1.1) spit out <table>'s for certain browsers.

Upvotes: 3

Related Questions