function ConfirmActive()
{
if(<%= TimeSheetFlag %> != -100) //do not save or cancel about the previous item
{
if (confirm("Are you sure you want to delete the custom search?") == false)
{
SetCancel();//请注意这儿
}
}
return true;
}

function SetCancel()
{
alert("sdd");//这儿

var cancel = document.getElementById("cmdSetCancel"); //服务器端控件
cancel.click(); //调用点击事件.

alert("add");////这儿
}
问题是这样的:如果我把alert(sdd)...add加上,服务器端事件就可以正常触发,如果去掉就不触发了.真是奇怪!
请帮忙.

解决方案 »

  1.   

    做了个例子,大家一起研究:(页面只有imagebutton and button) ------就会重现我说的问题
    .cs
    protected System.Web.UI.WebControls.ImageButton ImageButton1;
    protected System.Web.UI.WebControls.Button Button1;
    private void Page_Load(object sender, System.EventArgs e)
    {
    ImageButton1.Attributes.Add("onclick","return CC();");
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
    this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Button1_Click(object sender, System.EventArgs e)
    {
    int xx = 1;
    } private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
    int yy = 2;
    }.aspx<script language = "javascript">
    function CC()
    {
    //alert("sdd");
    document.all.Button1.click();
    //alert("add");
    }
    </script>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:ImageButton id="ImageButton1" style="Z-INDEX: 101; LEFT: 384px; POSITION: absolute; TOP: 224px"
    runat="server"></asp:ImageButton>
    <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 544px; POSITION: absolute; TOP: 344px" runat="server"
    Text="Button"></asp:Button>
    </form>
      

  2.   

    这么触发服务器端事件可能有问题,因为有时候asp.net是通过__doPostBack回送,有时候是把asp:button 在客户端 变成<input type=submit...>回送先用
    string strScript = GetPostBackEventReference(ImageButton1)获得控件引起会送的js语句,再处理
      

  3.   

    ImageButton 也是一种表单控件,点击这种按钮时,也会回传到服务器,点击它时如果不想让它回传可以在函数中添加一条 return false 语句,这样 imagebutton 点击时就不会往服务器回传了。function CC()
    {
    document.all.Button1.click();
    return false;
    }
      

  4.   

    你那个cmdSetCancel是submit还是普通button?如果是submit,你直接执行form.submit就行了,如果是普通button,那么你执行它的click没意思啊,不如直接调用__doPostBack。