【问题标题】:ASP.NET 2.0: Calling a javascript function from onChange with AutoPostBack=trueASP.NET 2.0:使用 AutoPostBack=true 从 onChange 调用 javascript 函数
【发布时间】:2010-10-12 16:39:21
【问题描述】:

当我查看源代码时,我有一个 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(),我想知道 __doPostBack() 是否可以在 DoCustomStuff() 完成其工作之前执行。

【问题讨论】:

    标签: asp.net drop-down-menu onchange autopostback


    【解决方案1】:

    尝试像这样手动附加回发参考:

    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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-05
      • 2011-04-30
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      • 2011-08-13
      • 2011-01-20
      相关资源
      最近更新 更多