最近在练习使用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)
        {        }
    }
}

解决方案 »

  1.   

    你是怎么导致回转的?你pageload里说做了两次是不是postback也执行了 img_1.Style.Add("width", "400px"); ?
      

  2.   

    <asp:image id="img_1" runat="server" onclick="this.style.width=this.offsetWidth+1+'px'"> </asp:image> 
    如果是runat server,这个onclik应该是一个服务端的onclick,如果是客户端我记得是onclientclick,或者在代码里加它的attribute属性。
      

  3.   

    回传是通过按钮Button1回传得,根据在page_load里面断点执行了两次。如果把Image控件屏蔽了,则只会执行一次。很奇怪。
      

  4.   

    onclick="this.style.width=this.offsetWidth+1+'px'"去掉了也是一样的,不是这个的缘故,这个是我在做其他操作的时候加上去的,对于服务端事件是没有影响的。
      

  5.   

    是不是控件属性里对image进行了设置啊
      

  6.   

    没有对Image属性作任何设置,应该是Image类库里面做的操作。但是不知道为什么会这样。研究了一天了也没有头绪。
      

  7.   

    在用2008时我也碰到过这样的问题.不知为什么,可能是2008中Image控件自身的原因.
      

  8.   

    给Image的ImageUrl属性赋个非空串就OK了。
    http://www.jzquan.com/a/prog/comp/200812253945.html