1

以下 .NET 4.0 代码在 IE9、Chrome 20 和 Opera 12 中完美运行,但在 Firefox 13 中却惨遭失败。所有浏览器都是默认安装,没有插件。

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"><title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <asp:TreeView ID="Treeview1" Runat="server" 
            onclick="javascript:postBackByObject()"
            ontreenodecheckchanged="Treeview1_TreeNodeCheckChanged" 
            ShowCheckBoxes="All" >
            <Nodes>
              <asp:TreeNode Value="Child1" Expanded="True" Text="1">
                <asp:TreeNode Value="Grandchild1" Text="A" />
                <asp:TreeNode Value="Grandchild2" Text="B" />
             </asp:TreeNode>
               <asp:TreeNode Value="Child2" Text="2" />
               <asp:TreeNode Value="Child3" Expanded="True" Text="3">
               <asp:TreeNode Value="Grandchild1" Text="A" />
             </asp:TreeNode>
            </Nodes>
       </asp:TreeView>
       <asp:TextBox ID="TextBox1" runat="server" Height="156px" TextMode="MultiLine" 
            Width="295px"></asp:TextBox>
    </div>
    </form>
 </body>
  <script language="javascript" type="text/javascript">
      function postBackByObject() {
         var o = window.event.srcElement;
         if (o.tagName == "INPUT" && o.type == "checkbox") {
             __doPostBack("", "");
         }
     }
 </script>
 </html>

我正在尝试使用以下代码捕获 TreeNode 复选框事件:

  protected void Treeview1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
  {
     TextBox1.Text += e.Node.Text;
  }

这很烦人 - 欢迎任何解决方案!

4

1 回答 1

1

try handling the event more generically, so it works in both FF and other browsers.

e.g.

onclick="javascript:postBackByObject(event)"

function postBackByObject(e) {
    var evt = e || window.event;
    var o = evt.target || evt.srcElement; 
    if (o.tagName == "INPUT" && o.type == "checkbox") {
        __doPostBack("", "");
    }
}

edit i forgot to add a line for target / srcElement

于 2012-07-15T21:22:39.807 回答