TreeView 控件之不回发操作

不要求回发,只能使用客户端的脚本来实现了,下面就是完整的代码。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

 protected void Page_Load(object sender, EventArgs e)
 {
  Response.Write("刷新标记:" + DateTime.Now.Ticks.ToString());
  TreeView1.Attributes.Add("onclick", "getNode(event);");
 }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title>TreeView 控件之不回发操作</title>

 <script type="text/javascript">
 var foo = null;
function getNode(evt)
{
 evt1 = window.event ? window.event.srcElement : evt.target;
 if(evt1.tagName && evt1.tagName != "INPUT")
 {
      if(evt1.tagName == "IMG")
      {      
        return true;
      }
      else
      {
       if(foo) foo.style.color="#00F"; //最初的颜色
        evt1.style.color="#f0f0f0"; //如果要其他单击变换颜色,调整这3的位置即可,应该不用再说了吧???
        foo = evt1;
       
        var sb = evt1.previousSibling;
        if(sb.tagName == "INPUT")
        {
          var imgPlus = document.getElementById(sb.id.replace(/CheckBox/gi,""));
          if(imgPlus)
          {
            eval(imgPlus.href)
          }
        }
        if(window.event)
        {
         window.event.returnValue=false;
         return false;
        }
        else
        {
         evt.preventDefault();
         evt.stopPropagation();
        }
    }
 }
}

 </script>

</head>
<body>
 <form id="form1" runat="server">
 <div>
  <asp:TreeView ID="TreeView1" runat="server" EnableClientScript="True" ShowCheckBoxes="All" ShowLines="True">
   <Nodes>
    <asp:TreeNode Text="ASP.NET">
     <asp:TreeNode Text="ASP.NET MVC">
      <asp:TreeNode Text="Silverlight">
       <asp:TreeNode Text="LINQ">
        <asp:TreeNode Text="XML"></asp:TreeNode>
       </asp:TreeNode>
      </asp:TreeNode>
     </asp:TreeNode>
     <asp:TreeNode Text="XSL">
      <asp:TreeNode Text="C#">
       <asp:TreeNode Text="VB.NET"></asp:TreeNode>
      </asp:TreeNode>
     </asp:TreeNode>
     <asp:TreeNode Text="Blog">
      <asp:TreeNode Text="新闻"></asp:TreeNode>
     </asp:TreeNode>
    </asp:TreeNode>
    <asp:TreeNode Text="javaScript"></asp:TreeNode>
    <asp:TreeNode Text="HTML DOM">
     <asp:TreeNode Text="VSTS">
      <asp:TreeNode Text="Live">
       <asp:TreeNode Text="Book On Line"></asp:TreeNode>
      </asp:TreeNode>
     </asp:TreeNode>
    </asp:TreeNode>
   </Nodes>
  </asp:TreeView>
 </div>
 </form>
</body>
</html>

posted @ 2008-06-05 21:04  mFrog  阅读(685)  评论(0编辑  收藏  举报