今天在做的一个报表系统遇到了屏幕分辨率的问题,最后采用了通过获取屏幕分辨率来动态设置页面控件属性的办法来解决

记录代码(以作备用):\

前台代码:

代码
<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=" height:100%; 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;
}
}
}

 以上来自:http://www.cnblogs.com/00OO/archive/2010/08/20/1804334.html

我自己采用的方法大同小异,也是用hidden控件记录客户端值,用button控件提交。

前台:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
</head>
<body id="MyBody" runat="server">
<form id="form1" runat="server">
<table><tr><td>
<input id="screenWidth" name="screenWidth" type="hidden" runat="server" />
</td></tr>
</table>
</form>
</body>
</html>

后台: 

protected void Page_Load(object sender, EventArgs e)
{
System.Web.UI.WebControls.Button Button1
= new System.Web.UI.WebControls.Button();
if (!IsPostBack)
{
Button1.ID
= "Button1";
this.FindControl("form1").Controls.Add(Button1);
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();
}
Response.write(
"<script>alert('"+ screenWidth.Value +"')</script>");
}
}
posted on 2011-07-19 14:19  飞翔-方向 积累 沉淀  阅读(3384)  评论(0)    收藏  举报