473,396 Members | 2,121 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
Nov 19 '05 #1
2 2494
"Steve Franks" <pl****@postreplyhere.com> wrote in
news:F4*****************************************@c omcast.com:
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.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.

Newmarket Volvo Sucks! http://newmarketvolvo.tripod.com
Nov 19 '05 #2
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
"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn**************************@127.0.0.1...
"Steve Franks" <pl****@postreplyhere.com> wrote in
news:F4*****************************************@c omcast.com:
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.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.

Newmarket Volvo Sucks! http://newmarketvolvo.tripod.com

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Randell D. | last post by:
HELP! I am determined to stick with this... I'm getting there... for those who haven't read my earlier posts, I'm createing what should be a simple function that I can call to check that...
0
by: Burak | last post by:
Hello, This is my code behind code for dynamic link button creation: strUrl = "<asp:LinkButton CommandName='Onet3_Cd' onfiltered='LinkButton_Command' runat='server' " & _ "Text='" &...
1
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a...
4
by: sydney.luu | last post by:
Hello, I would greatly appreciate if someone can show me how to dynamically build a Repeater with unknown number of columns at design time. I have looked various threads in this newsgroup,...
10
by: Ron | last post by:
Hi, I currently am generating a report by to my end user by retrieving data from my back end DB and concatenating strings together and place this string concatenation within a Label control. I...
2
by: tshad | last post by:
Is there a way to dynamically add in User Controls where I would get the name from a session variable: If I have the following code:...
8
by: SalamElias | last post by:
I create 2 groupes of checkboxes dynamically (I don't use checkboxlist) for 2 differnet zones in my aspx web page and I assign a "onClick" javascript handler by group., lets say the first 4 chboxes...
0
by: nedumsb | last post by:
Hi, How can we add serverside controls dynamically in a repeater control and access them in code behind? we can simple add a single (or known no of) server controls in <ItemTemplate> like...
3
by: Allen Chen [MSFT] | last post by:
Hi Richard, Quote from Richard================================================== However I also want to be able to remove the panes. I have tried to include this, but find that when I first...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.