Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 143181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:03:17+00:00 2026-05-11T08:03:17+00:00

I have been learning how to create dynamic images and dynamic stylesheets using ASP.NET

  • 0

I have been learning how to create dynamic images and dynamic stylesheets using ASP.NET 3.5. My reasoning behind this is I have a web page that I want to change the header background image (set with css) automatically. Check below for my test script:


<%@ Page Language='VB' AutoEventWireup='false' CodeFile='Default.aspx.vb' Inherits='_Default' %>  <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> <html xmlns='http://www.w3.org/1999/xhtml'> <head runat='server'>     <title></title>     <%         Response.Output.WriteLine('<link rel=''Stylesheet'' type=''text/css'' href=''style.aspx?t={0}&v={1}'' />', oType, oText)     %> </head> <body>     <form id='form1' runat='server' action='Default.aspx'>         <asp:ScriptManager ID='ScriptManager1' runat='server' />         <asp:UpdatePanel ID='UpdatePanel1' runat='server'>             <ContentTemplate>                 <div class='testheader'>&nbsp;</div>                 <asp:Label ID='Label1' runat='server' Text='Label'></asp:Label> <%-- for testing --%>             </ContentTemplate>             <Triggers>                 <asp:AsyncPostBackTrigger ControlID='Button1' EventName='Click' />             </Triggers>         </asp:UpdatePanel>         <asp:Button ID='Button1' runat='server' Text='Button' />     </form> </body> </html> 

So nothing special about the default form page above, it has an DIV styled to have the dynamic background image and a Label, which as the comment indicates is just to make sure my AsyncPostBack is functioning properly.

Partial Class _Default     Inherits System.Web.UI.Page     Public oType As String = 'm'     Public oText As String = 'Genius on the Web'     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click         Select Case oType             Case 'm'                 oType = 'c'             Case 'c'                 oType = 'm'         End Select         Label1.Text = Now.ToString     End Sub End Class 

Again, nothing fancy. Just swaps the two values I have temporarily hard-coded into the program, and updates the Label text.

This is my dynamic stylesheet:

<%@ Page Language='VB' %>  <%       Response.ContentType = 'text/css'     Dim qString As String = Request.QueryString('t')     Dim bText As String = Request.QueryString('v')     If String.IsNullOrEmpty(qString) Then qString = 'blank'     If String.IsNullOrEmpty(bText) Then bText = 'Placeholder'     Dim theColor, H1size, bannerImg As String     Select Case qString         Case 'c'             theColor = 'green'             H1size = 30         Case 'm'             theColor = 'blue'             H1size = 26         Case Else             theColor = 'red'     End Select     bannerImg = String.Format('image.aspx?t={0}&p={1}', Server.UrlEncode(bText), qString)      %>  body { background-color: <%=theColor%>; } .testheader { background: url(<%=bannerImg%>); background-repeat:no-repeat; height:120px; } .testclass { font-size: <%=H1size%>px; border:1px solid yellow; margin-bottom:2em; } 

Finally, here is my dynamic image script:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load     Response.ContentType = 'image/jpeg'     Response.Clear()     Response.BufferOutput = True       Try         Dim oText As String = Server.HtmlDecode(Request.QueryString('t'))         If String.IsNullOrEmpty(oText) Then oText = 'Placeholder'          Dim oPType As String = Server.HtmlDecode(Request.QueryString('p'))         If String.IsNullOrEmpty(oPType) Then oPType = 'none'          Dim imgPath As String = ''          Select Case oPType             Case 'c'                 imgPath = 'img/banner_green.jpg'             Case 'm'                 imgPath = 'img/banner_blue.jpg'             Case Else                 Throw New Exception('no ptype')         End Select          Dim oBitmap As Bitmap = New Bitmap(Server.MapPath(imgPath))          Dim oGraphic As Graphics = Graphics.FromImage(oBitmap)         Dim frontColorBrush As New SolidBrush(Color.White)          Dim oFont As New Font(FONT_NAME, 30)          Dim oInfo() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders         Dim oEncoderParams As New EncoderParameters(1) 'jpeg         Dim xOffset As Single = Math.Round((oBitmap.Height - oFont.Height) / 2, MidpointRounding.ToEven)          Dim oPoint As New PointF(275.0F, xOffset + 10)          oEncoderParams.Param(0) = New EncoderParameter(Encoder.Quality, 100L)          oGraphic.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias         oGraphic.DrawString(oText, oFont, frontColorBrush, oPoint)          oBitmap.Save(Response.OutputStream, oInfo(1), oEncoderParams)          Response.Output.Write(oBitmap)          oBitmap.Dispose()         oGraphic.Dispose()          Response.Flush()     Catch ex As Exception      End Try End Sub 

So armed with this information, I want to know if it is possible for the AsyncPostBack refresh the CSS so that the image will change when I click Button2. I am open for suggestions and/or ‘thats the stupid/hard way to do this, try this…’ type of feedback.

Thanks guys!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:03:17+00:00Added an answer on May 11, 2026 at 8:03 am

    Since you said you’re open to suggestions… why are you set on doing this with the AsyncPostBack and CSS? Why not just have a javascript onclick event to dynamically change the image when you click Button2?

    edit (in response to post below):

    There would be no post back (if that’s what you mean by flickering): you can still use the AsyncPostBack for whatever else you’re doing, and then have an additional javascript function fired off onclick that would do something like

    document.getElementById('headerimg').src='2.jpg'; 

    This would change the image to new source file without any page refresh.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 78k
  • Answers 78k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Double-checked locking has been proven to be incorrect and flawed… May 11, 2026 at 3:51 pm
  • added an answer You should create one image with a width of 50px… May 11, 2026 at 3:51 pm
  • added an answer I can't guarantee this is the best way, but it's… May 11, 2026 at 3:51 pm

Related Questions

I'm new to MSBuild, and am learning as I need to know how to
I'm working on a simple application to start learning my way around WPF. I
I'm trying to wrap my head around asp.net. I have a background as a
I have been slowly learning SQL the last few weeks. I've picked up all

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.