How to dynamically write html based on a var from code behind?

S

Steve Franks

What is the proper/best way to dynamically change the HTML written out to
the browser based on the value from a variable in a code behind page?

For instance, I have test.aspx.vb as a code behind page. In this code
behind's Page_Load event which sets a boolean called useAlternateStyle to
true or false based on business logic.

Now from my main page (test.aspx) I want to either write out HTML that sets
the style sheet to "mainStyle.css" or "alternateStyle.css" depending on the
value of this useAlternateStyle variable.

What is the best way to do this?

One way I know is to create the useAlternateStyle variable as a public or
protected variable within the code behind's class. And then in my .aspx
file I could do this in the HEAD section:

<%
if useAlternateStyle = true then
Response.Write("<link href=""alternateStyle.css"" rel=""stylesheet""
type=""text/css"" />")
else
Response.Write("<link href=""mainStyle.css"" rel=""stylesheet""
type=""text/css"" />")
end if
%>

However this seems like the old school way of doing this sort of thing. Is
there a better way?

For example, I'd prefer to somehow have a control that does a runat=server
or something that can dynamically set the name of the stylesheet. Like:
<asp:somecontrol value=getStyleSheetNameToUseFromVariable runat=server>
or something like this.

Any advise? Thanks very much in advance.

Steve
 
L

Lucas Tam

Now from my main page (test.aspx) I want to either write out HTML that
sets the style sheet to "mainStyle.css" or "alternateStyle.css"
depending on the value of this useAlternateStyle variable.

What is the best way to do this?

If you only want to switch the style sheet, then add "runat=server"
property to the link control... now you'll have access to it in the code
behind.

Otherwise take a look at the literal control and HTMLGeneric Control.
 
S

Steve Franks

Thanks. However I do not see a "link" control. I have a LinkButton and
Hyperlink control, but don't think that's it.

Can you please clarify specifically what you mean by link control? Also can
you provide a one line snippet that demonstrates how a link control would be
set to a value based on a variable set within a code behind page?

Thanks,

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top