Vb.net 是否可以使用AJAX回发刷新CSS工作表?
我一直在学习如何使用ASP.NET3.5创建动态图像和动态样式表。我的理由是我有一个网页,我想改变标题背景图像(设置css)自动。检查下面的测试脚本:Vb.net 是否可以使用AJAX回发刷新CSS工作表?,vb.net,asp.net-ajax,asp.net-3.5,dynamic-css,Vb.net,Asp.net Ajax,Asp.net 3.5,Dynamic Css,我一直在学习如何使用ASP.NET3.5创建动态图像和动态样式表。我的理由是我有一个网页,我想改变标题背景图像(设置css)自动。检查下面的测试脚本: 再说一次,没什么特别的。只需交换我临时硬编码到程序中的两个值,并更新标签文本 这是我的动态样式表: <%@ Page Language="VB" %> <% Response.ContentType = "text/css" Dim qString As String = Request.QueryStr
再说一次,没什么特别的。只需交换我临时硬编码到程序中的两个值,并更新标签文本 这是我的动态样式表:
<%@ 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; }
有了这些信息,我想知道AsyncPostBack是否可以刷新CSS,这样当我点击按钮2时图像就会改变。我愿意接受建议和/或“这是做这件事的愚蠢/艰难的方式,试试这个…”类型的反馈
谢谢大家 既然你说你愿意接受建议。。。为什么要用AsyncPostBack和CSS来实现这一点?为什么不在单击按钮2时使用javascript onclick事件动态更改图像 编辑(回应以下帖子): 不会有回发(如果这就是你所说的闪烁的意思):你仍然可以对你正在做的任何事情使用AsyncPostBack,然后在onclick上启动一个额外的javascript函数
document.getElementById('headerimg').src='2.jpg';
这将在不刷新任何页面的情况下将图像更改为新的源文件。因此我发现:因为我在Default.aspx页面上设置了变量,所以每次单击按钮时,它都会将变量重置为默认值,使其看起来好像不起作用。我把变量移到了一个外部类中,一切都很顺利。谢谢
<%@ 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; }
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
document.getElementById('headerimg').src='2.jpg';