C#后台代码获取前台aspx中中width值

mszhoujy 2008-08-25 04:39:38
如下:
前台a.aspx中
..............
<div>
<img id="test" onclik="alert(document.getElementById('test').width)" style="width:100% height:100%" />
</div>...............
后台a.aspx.cs中
protected void page_load(...)
{
int i=test.width; //此处为-1,未取得img的正确值
}

如上,在vs2005中开发(.net2.0),在前台的弹出事件中值是对的,但在后台代码中通过id.width取不到值,但是如果前台如下写法<img id="test" width="800px" height="600px">貌似后台test.width是正确的。
我现在的问题是现在前台<img>的大小不能确定,在客户端不同分辨率下自适应充满整个上层容器(如div),所以不能设为具体的px值,而设为100%,但在后台我还需要获得其尺寸来设置另一COM组件的大小(该组件需要并只能使用分辨率值来设置),所以该如何获得?
有人会说若你不指定img具体px大小,在page_load事件中触发时还不知道img的具体大小,应该在窗口绘完后才能计算得到img的width和height,问题是我又添加了一个button,点击后获得img值仍为-1,。。。。。。。。。
高手指点 !
...全文
622 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiangzhuo93 2008-08-27
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 mszhoujy 的回复:]
4楼的兄弟我知道是对的,但问题是width值不能指定为具体px值,只能使用100%来适应上层容器,以适应不同分辨率,此时获得的test.Style["width"]值为100%,对我后台是没有任何意义的。
6楼的方法我试了一下,这是运行后客户端页面源文件:
<!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" >
<…
[/Quote]


ID 和 ClintID
zhaokun521 2008-08-26
  • 打赏
  • 举报
回复
前台:
function show()
{
var obj = document.getElementById("img1");
var ss = obj.width;
var id = document.getElementById("hfwd");
id.value = ss;
}
<body onload="show()">
<img id="img1" src="image/images/barner01.jpg" width="80px"/>
<asp:hiddenfield runat="server" ID="hfwd"></asp:hiddenfield>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

后台
protected void Button1_Click(object sender, EventArgs e)
{
int i= 0;
if (this.hfwd.Value != null && this.hfwd.Value.ToString() != "")
{
i = Convert.ToInt32(this.hfwd.Value.Trim());
}
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + i.ToString() + "')</script>");
}
是可以的,方法很多,看怎么用和想怎么用了
zds1010 2008-08-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ZengHD 的回复:]
楼主再试一次

HTML code
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 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 id="Head1" runat="server">
<title>无标题页 </title>
<script type="text/javascript…
[/Quote]
http://www.xpeng.com.cn
http://www.lyyywx.com.cn
http://www.bdgxqsq.com.cn
ZengHD 2008-08-26
  • 打赏
  • 举报
回复
楼主再试一次

前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 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 id="Head1" runat="server">
<title>无标题页 </title>
<script type="text/javascript">
function GetImgWidth()
{
document.getElementById("hidImgWidth").innerText = document.getElementById('MainMapIMG').width;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input runat="server" id="hidImgWidth" type="hidden" />
<img id="MainMapIMG" onclick="alert(document.getElementById('MainMapIMG').width)" style="z-index: -999; left: 0px;position: absolute; top: 24px; width:100%; height:100%" align="middle" name="MainMapIMG" runat="server" controls="true" />  
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" OnClientClick="GetImgWidth();" /> </div>
</form>
</body>
</html>



后台:
protected void Button1_Click(object sender, EventArgs e)
{
int i = Convert.ToInt32(hidImgWidth.Value);
Response.Write(i.ToString());
}
lijin84100 2008-08-26
  • 打赏
  • 举报
回复
<img id="test" src="http://zi.csdn.net/300x60_15k.gif" runat="server" onclik="alert(document.getElementById('test').width)" style="width: 353px" />

Response.Write(test.Style["width"].ToString())这种是对的.
lijin84100 2008-08-26
  • 打赏
  • 举报
回复
<img id="test" src="http://zi.csdn.net/300x60_15k.gif" runat="server" onclik="alert(document.getElementById('test').width)" style="width: 353px" />

Response.Write(test.Style["width"].ToString());
mszhoujy 2008-08-26
  • 打赏
  • 举报
回复
我把详细代码贴出来
Default2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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>
</head>
<body>
<form id="form1" runat="server">
<div>

<img id="MainMapIMG" onclick="alert(document.getElementById('MainMapIMG').width)" style="z-index: -999; left: 0px;position: absolute; top: 24px; width:100%; height:100%" align="middle" name="MainMapIMG" runat="server" controls="true" />  
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
</form>
</body>
</html>


Default2.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int i = Convert.ToInt32(this.MainMapIMG.Attributes["width"]);

}

protected void Page_PreRender(object sender, EventArgs e)
{
int i = Convert.ToInt32(this.MainMapIMG.Attributes["width"]);

}

protected void Button1_Click(object sender, EventArgs e)
{
int i = Convert.ToInt32(this.MainMapIMG.Attributes["width"]);

}
}

我试了下,在3个事件中i都为0,this.MainMapIMG.Attributes["width"]也为空,2楼兄弟再看看,3楼兄弟我得width值不能指定为具体px值,只能使用100%来适应上层容器,以适应不同分辨率,若将style中width改为100%,则用test.Style["width"]得到的值也是100%,是没有用的,麻烦在给看下阿,貌似在aspx中可以得到这个值,但在后台cs中C#代码得不到具体值。
在明白一点,我在<img>中用100%来设置style中width的值,在后台要得到其在具体客户端屏幕的具体像素px值。该如何实现,我觉得实在不行可以想象其他办法,比如现保存在js代码中,再想办法显式传回C#代码中,当然我是不杂明白,可能实现不了,总之达到上目的就可以。
mszhoujy 2008-08-26
  • 打赏
  • 举报
回复
要沉,自己顶一下,高手们在给看一看啊,谢谢。
mszhoujy 2008-08-26
  • 打赏
  • 举报
回复
4楼的兄弟我知道是对的,但问题是width值不能指定为具体px值,只能使用100%来适应上层容器,以适应不同分辨率,此时获得的test.Style["width"]值为100%,对我后台是没有任何意义的。
6楼的方法我试了一下,这是运行后客户端页面源文件:
<!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><title>
无标题页
</title>
<script language="javascript" id="clientEventHandlersJS" type="text/jscript">
function GetImgSize()
{
document.getElementById("Midimg").value=document.getElementById("MainMapIMG").width;
debugger;
}


</script>
</head>
<body>
<form name="form1" method="post" action="Default2.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNTczMjAxMTUzZGQlu0lAhEcnBmAnPQ+cx9Dfu73D+A==" />
</div>

<div>


<img id="MainMapIMG" onclick="GetImgSize();" style="z-index: -999; left: 0px;position: absolute; top: 24px; width:100%; height:100%" align="middle" name="MainMapIMG" controls="true" />  
<input type="submit" name="Button1" value="Button" id="Button1" />
</div>

<div>

<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgLNy6eVCgKM54rGBtzacJDLyzBvUNFXUJb8wpGCSnCM" />
</div></form>
</body>
</html>


其中<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgLNy6eVCgKM54rGBtzacJDLyzBvUNFXUJb8wpGCSnCM" />应该就是我们添加的<input>对象,所以在点击后vs2005给出了document.getElementById("Midimg")引用对象为空的错误,也就是说服务器端控件(加runat=server的input)在客户端生成了一个临时对象,id已经被修改,所以。。。结果可想而知。
7楼兄弟的意思和6楼大致一样。
总之,还是没有成功,大家在帮忙看看,兄弟在这先谢了!
ZengHD 2008-08-25
  • 打赏
  • 举报
回复
 <img id="test" src="http://zi.csdn.net/300x60_15k.gif" runat="server" onclik="alert(document.getElementById('test').width)" style="width: 353px"  /> 

Response.Write(test.Style["width"].ToString());
newtypebao 2008-08-25
  • 打赏
  • 举报
回复

前台

<img id="myimg" runat="server">
后台

protected htmlgenericcontrol myimg;

int i=this.myimg.attributes["width"];

(1)前台标签runat=server是必须的
(2)HtmlGenericControl的命名空间是System.Web.UI.HtmlControls

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧