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

Discussion in 'ASP .Net' started by Steve Franks, Aug 31, 2005.

  1. Steve Franks

    Steve Franks Guest

    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
     
    Steve Franks, Aug 31, 2005
    #1
    1. Advertising

  2. Steve Franks

    Lucas Tam Guest

    "Steve Franks" <> wrote in
    news::

    > 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 ()
    Please delete "REMOVE" from the e-mail address when replying.

    Newmarket Volvo Sucks! http://newmarketvolvo.tripod.com
     
    Lucas Tam, Aug 31, 2005
    #2
    1. Advertising

  3. Steve Franks

    Steve Franks Guest

    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" <> wrote in message
    news:Xns96C3B13F4733nntprogerscom@127.0.0.1...
    > "Steve Franks" <> wrote in
    > news::
    >
    >> 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 ()
    > Please delete "REMOVE" from the e-mail address when replying.
    >
    > Newmarket Volvo Sucks! http://newmarketvolvo.tripod.com
     
    Steve Franks, Aug 31, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    630
    Alphonse Giambrone
    Jun 28, 2003
  2. Alvin Bruney

    Threads.. Session var lost, App var ok

    Alvin Bruney, Dec 2, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    378
    rooster575
    Dec 2, 2003
  3. ntm
    Replies:
    2
    Views:
    4,733
    Teemu Keiski
    Jan 19, 2004
  4. thomson
    Replies:
    10
    Views:
    2,521
    Eliyahu Goldin
    Jun 20, 2005
  5. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    521
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
Loading...

Share This Page