asp.net获取屏幕分辨率,并根据分辨率设置动态设置页面上控件属性
时间:2010-08-20 来源:O零O
今天在做的一个报表系统遇到了屏幕分辨率的问题,最后采用了通过获取屏幕分辨率来动态设置页面控件属性的办法来解决
记录代码(以作备用):\
前台代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body id="MyBody" runat="server">
<form id="form1" runat="server">
<table style=" width:100%" >
<tr>
<td align="center" valign="middle">
<asp:label id="Label1" runat="server"></asp:label>
<asp:Image ID="Image1" ImageUrl="~/123.jpg" ImageAlign="Middle" runat="server" />
</td>
</tr>
</table>
</form>
</body>
</html>
后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
System.Web.UI.WebControls.Button Button1 = new System.Web.UI.WebControls.Button();
if (!IsPostBack)
{
System.Web.UI.HtmlControls.HtmlInputHidden btnW = new System.Web.UI.HtmlControls.HtmlInputHidden();
Button1.ID = "Button1";
btnW.Name = "WidthPixel";
btnW.ID = "WidthPixel";
this.FindControl("form1").Controls.Add(btnW);
this.FindControl("form1").Controls.Add(Button1);
string scriptString = "";
scriptString += "document.all.form1.WidthPixel.value=window.screen.width;\r\n";
this.RegisterOnSubmitStatement("Meng", scriptString);
this.MyBody.Attributes.Add("onload", "document.all." + Button1.ClientID + ".click();");
}
else
{
this.MyBody.Attributes.Remove("onload");
if (this.FindControl("form1").Controls.Contains(Button1))
{
this.FindControl("form1").Controls.Remove(Button1);
Button1.Dispose();
}
System.Text.StringBuilder strLabel = new System.Text.StringBuilder();
strLabel.Append("您的浏览器分辨率宽为:");
strLabel.Append(Request.Form["WidthPixel"]);
Label1.Text = strLabel.ToString();
int Wth = int.Parse(Request.Form["WidthPixel"])/2;
this.Image1.Width = Wth;
}
}
}
出现问题:
解决方法:将桌面应用主题切换回Windows 经典模式;(但是在分辨率调整到800*600的时候仍然会出现改问题,因为最完美的解决办法还是去微软官方找补丁把)
相关阅读 更多 +