最近在练习使用VS2008,发现当一个web页面中加入了Image控件以后,我在后台代码Page_Load中加了断点,发现每次无论页面加载还是页面回传断点都会被执行两次(在Vs2005中不存在这种现象),而且对于按钮回传事件后第二次执行的Page_Load执行结果没有对于界面没有任何影响,不知道是什么原因,会不会是一个BUG,或者里面有什么玄机希望各位牛人赐教。下面是我界面和后台的源代码,希望各位看一下给些看法(如果觉得不明确我可以把程序源代码发给各位邮箱,希望大家一起来解决):
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:image id="img_1" runat="server" onclick="this.style.width=this.offsetWidth+1+'px'"></asp:image>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" Width="63px" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"
ontextchanged="TextBox1_TextChanged"></asp:TextBox>
</div>
</form>
</body>
</html>
Default.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)//断点加在这里 {
img_1.Style.Add("width", "400px");
} } protected void Button1_Click(object sender, EventArgs e)
{//断点加在这里
img_1.Style.Add("width", "200px");
} protected void Button2_Click(object sender, EventArgs e)
{ } protected void TextBox1_TextChanged(object sender, EventArgs e)
{ }
}
}
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:image id="img_1" runat="server" onclick="this.style.width=this.offsetWidth+1+'px'"></asp:image>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" Width="63px" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"
ontextchanged="TextBox1_TextChanged"></asp:TextBox>
</div>
</form>
</body>
</html>
Default.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)//断点加在这里 {
img_1.Style.Add("width", "400px");
} } protected void Button1_Click(object sender, EventArgs e)
{//断点加在这里
img_1.Style.Add("width", "200px");
} protected void Button2_Click(object sender, EventArgs e)
{ } protected void TextBox1_TextChanged(object sender, EventArgs e)
{ }
}
}
如果是runat server,这个onclik应该是一个服务端的onclick,如果是客户端我记得是onclientclick,或者在代码里加它的attribute属性。
http://www.jzquan.com/a/prog/comp/200812253945.html