代码之家  ›  专栏  ›  技术社区  ›  tex

ASP.NET 2.0:使用AutoPostBack=true从onChange调用javascript函数

  •  0
  • tex  · 技术社区  · 17 年前

    我有一个ASP.NET DDL,在查看源代码时如下所示:

    <select name="testControl" onchange="DoCustomStuff();setTimeout('__doPostBack(\'testControl\',\'\')', 0)" id="testControl">
    

    在.cs页面上看起来是这样的:

    <asp:DropDownList ID="testControl" runat="server" onchange="DoCustomStuff()" OnSelectedIndexChanged="testControl_Changed" AutoPostBack="true" />
    

    有人看到在这样的DDL上使用onchange和AutoPostBack=“true”有问题吗?我这样问是因为我们有一些用户的DoCustomStuff()调用似乎不正确,我想知道是否有可能在DoCustomStuff()完成工作之前执行_udopostback()。

    1 回复  |  直到 17 年前
        1
  •  0
  •   Canavar    17 年前

    尝试手动附加回发引用,如下所示:

    Page.ClientScript.RegisterClientScriptBlock(
      typeof(_Default), 
      "PageScripts", 
      string.Format("function DoCustomStuff() { /* Your Code Here */ {0} }", Page.ClientScript.GetPostBackEventReference(testControl, string.Empty))
    );
    
    testControl.Attributes["onchange"] =  "DoCustomStuff();";
    

    Page.ClientScript.GetPostBackEventReference(testControl, string.Empty))