2011-10-07 66 views
1

我有一個Web窗體應用程序樹視圖。asp.net javascript:獲取樹形視圖的innertext

我有被調用每一個有樹視圖內的點擊時間javascript函數:

<script type="text/javascript"> 
       function OnTreeClick(evt) { 
        var src = window.event != window.undefined ? window.event.srcElement : evt.target; 
        var nodeClick = src.tagName.toLowerCase() == "a"; 
        if (nodeClick) { 
         var nodeText = src.innerText; 
         var nodeValue = GetNodeValue(src); 
         alert("Text: " + nodeText + "," + "Value: " + nodeValue); 
        } 
        //return false; //uncomment this if you do not want postback on node click 
       } 
       function GetNodeValue(node) { 
        //node value 
        var nodeValue = ""; 
        var nodePath = node.href.substring(node.href.indexOf(",") + 2, node.href.length - 2); 
        var nodeValues = nodePath.split("\\"); 
        if (nodeValues.length > 1) 
         nodeValue = nodeValues[nodeValues.length - 1]; 
        else 
         nodeValue = nodeValues[0].substr(1); return nodeValue; 
       } 
</script> 

問題:我如何吸引用戶是否點擊樹視圖中一個複選框?以及如何檢索旁邊的文本?

這裏是樹形的標記:

<asp:TreeView ID="TreeView1" runat="server" ShowLines="True" 
     onselectednodechanged="TreeView1_SelectedNodeChanged" 
     ontreenodecheckchanged="TreeView1_TreeNodeCheckChanged" CssClass="mytreeview" 
     > 
     <Nodes> 
      <asp:TreeNode Text="PreAnalytical" Value="PreAnalytical"> 
       <asp:TreeNode Text="Labels" Value="Labels"> 
        <asp:TreeNode Text="Specimen collection device mislabeled/unlabeled by practice" 
         Value="Specimen collection device mislabeled/unlabeled by practice" 
         ShowCheckBox="True"> 
        </asp:TreeNode> 
        <asp:TreeNode Text="Specimen mislabeled: in-house error (Lab or DE)" 
         Value="Specimen mislabeled: in-house error (Lab or DE)" 
         ShowCheckBox="True"></asp:TreeNode> 
       </asp:TreeNode> 
       <asp:TreeNode Text="Test Requisitions" Value="Test Requisitions"> 
        <asp:TreeNode Text="Missing: no form sent with specimen" 
         Value="Missing: no form sent with specimen" ShowCheckBox="True"></asp:TreeNode> 
        <asp:TreeNode Text="Wrong (i.e. OT instead of ORAL, sister practice)" 
         Value="Wrong (i.e. OT instead of ORAL, sister practice)" 
         ShowCheckBox="True"></asp:TreeNode> 
        <asp:TreeNode Text="Other: Non-ML" Value="Other: Non-ML" ShowCheckBox="True"></asp:TreeNode> 
        <asp:TreeNode Text="Copies Received: New ID/Req. assigned" 
         Value="Copies Received: New ID/Req. assigned" ShowCheckBox="True"></asp:TreeNode> 
        <asp:TreeNode Text="Incomplete/Blank Requisition Form" 
         Value="Incomplete/Blank Requisition Form" ShowCheckBox="True"></asp:TreeNode> 
        <asp:TreeNode Text="2 Specimens: 1 Req" Value="2 Specimens: 1 Req" 
         ShowCheckBox="True"> 
        </asp:TreeNode> 
        <asp:TreeNode Text="2 Reqs: 1 Specimen" Value="2 Reqs: 1 Specimen" 
         ShowCheckBox="True"> 
        </asp:TreeNode> 
       </asp:TreeNode> 
       <asp:TreeNode Text="Validity" Value="Need POC Results Confirmed"> 
        <asp:TreeNode Text="Need POC Results Confirmed" Value="New Node"></asp:TreeNode> 
        <asp:TreeNode Text="POC Results Marked Incorrectly" Value="New Node"> 
        </asp:TreeNode> 
        <asp:TreeNode Text="No Tests Ordered" Value="New Node"></asp:TreeNode> 
        <asp:TreeNode Text="SEC A Unclear" Value="New Node"></asp:TreeNode> 
       </asp:TreeNode> 
       <asp:TreeNode Text="Sales" Value="Sales"> 
        <asp:TreeNode Text="Practice is not entered in database" Value="New Node"> 
        </asp:TreeNode> 
        <asp:TreeNode Text="CP has not been updated" Value="New Node"></asp:TreeNode> 
       </asp:TreeNode> 
       <asp:TreeNode Text="Other" Value="Other"> 
        <asp:TreeNode Text="Other" Value="New Node"></asp:TreeNode> 
       </asp:TreeNode> 
      </asp:TreeNode> 
      <asp:TreeNode Text="Analytical" Value="Analytical"></asp:TreeNode> 
      <asp:TreeNode Text="Post-Analytical" Value="Post-Analytical"></asp:TreeNode> 
      <asp:TreeNode Text="Other" Value="Other"></asp:TreeNode> 
     </Nodes> 
    </asp:TreeView> 
+0

你能告訴您的'TreeView'一些標記? –

+0

@james更新謝謝 –

回答

1
function check_OnTreeNodeChecked(event) 
{ 
    var TreeNode = event.srcElement || event.target ; 
    if (TreeNode.tagName == "INPUT" && TreeNode.type == "checkbox") 
    { 
     if(TreeNode.checked) 
     { 
      //Do whatever here 
     } 
    } 
} 

然後用樹狀onclick事件附加此功能:

private void Page_PreRender(object sender, EventArgs e) 
{ 
    TreeView1.Attributes.Add("OnClick", "check_OnTreeNodeChecked(event)"); 
} 
+0

我將如何獲得treenode的文本? –

+0

TreeNode.innerHTML – HashTagDevDude

+0

非常感謝您的幫助。我這樣做:function check_OnTreeNodeChecked(event){var_ToolNode = event.srcElement || event.target;如果(TreeNode.tagName ==「INPUT」&& TreeNode.type ==「checkbox」){ if(TreeNode.checked){alert(TreeNode.innerHTML); } } }並得到空白字符串返回 –