Asp(C#) treeview 前台 後台跳轉關聯,TMD,終於弄出來了

來源:互聯網
上載者:User

   跟同學一塊,用了好幾天的時間,總算處理好這個問題了,昨天以為解決了,但是今天用的時候又發現漏洞了,今天中午+下午用了好久時間,總算摸索出來了,謝謝little sweat同學……

     目的:asp頁面,捕捉左側架構頁面中樹形菜單所選中內容,然後將對應的頁面給裝載到某個指定的架構內顯示,同時將所選內容傳過去……

方法一:(昨天的方法,後來證明這個方法有漏洞)

<!--這段代碼,可以捕捉頁面上treeview控制項中checkedbox為選中狀態的文本,但是悲劇的是,當把treeview上checkbox選中後再摺疊起來,他就愣了,找不到文本了-begin->

<script type="text/javascript">
        function check(){
            var html="";
            var check = document.getElementById("TreeView1").getElementsByTagName("input");
            for(var i=0;i<check.length;i++){
                if(check[i].checked)
                    html+=check[i].title+" ";
                   
            }
           // window.top.frames["left"].document.getElementById("text1").value=html;
          //parent.frames("right").frames("mainframe").location.href  ="MainWindow.aspx?hrz_heng=" +"<%=Session["request"]%>";
            parent.frames("right").frames("mainframe").location.href  ="MainWindow.aspx?hrz_heng=" +html;
        }

    </script>

<!------------------------------------------------------end--------------------------------------------------------------------------------------------->

方法二:

<!---------這段代碼是點擊複選框觸發,如果物件類型為checkbox 對象名為input即checkbox,則強制後台執行-------begin------>

    <script language="javascript" type="text/javascript">
     // 點擊複選框時觸發事件
    function postBackByObject()
      {
          var o = window.event.srcElement;
          if (o.tagName == "INPUT" && o.type == "checkbox")
          {
             __doPostBack("","");
          }
       }
    </script>

<!-----------------------------------------------------------------end-------------------------------------------------->

asp中的treeview控制項:

<<<<<<<<<<<<<<<<<<<<begin<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

 <asp:TreeView ID="TreeView1" runat="server"
                  ImageSet="XPFileExplorer"  EnableClientScript="False"
                           Width="250px" ShowCheckBoxes="leaf" Font-Size="Medium" 

                             ShowLines="True"  AutoGenerateDataBindings="True"
            ontreenodecheckchanged="TreeView1_TreeNodeCheckChanged" > 
            <SelectedNodeStyle Font-Overline="False" BackColor="BurlyWood" Font-Bold="true"/>
       </asp:TreeView>

<<<<<<<<<<<<<<<<<<<<<<<<end,<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

......................................................以上是前台代碼。。。。。。。。。。。。。。。。。。。。。。。。

。。。。。。。。。。。。。後台代碼。。。。。。。。。。。。。。。。。。。。。。。。。。。。

在treeview控制項的屬性中添加新的onclick屬性:

TreeView1.Attributes.Add("onclick", "postBackByObject()");

<!-------------------------------------一下代碼是從前台強制過來執行的--------------------------------------------------------->

 protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
    {
      
        string a = "";
        if (Session["request"] != null)
        {
            a = Session["request"].ToString();
        }
        //選中
        if (e.Node.Checked == true)
        {
            a += e.Node.Text + ",";
        }
        //取消
        if (e.Node.Checked == false)
        {
            a = a.Replace(e.Node.Text + ",", "");
        }
        Session["request"] = a;
      //  Response.Write(" <script> parent.frames('right').frames('mainframe').location.href  ='MainWindow.aspx?hrz_heng=' +'<%=Session['request']%>';</script> ");
        //Response.Write(" <script> parent.frames('right').frames('mainframe').location.href  ='shit.aspx';</script> ");

    }

<---------------------------------------end------------------------------------------------------------------------------------------>

點評:這個方法 很好,可以實現效果,但是美中不足的是,每點中一下checkbox,頁面總要重新整理一次,頁面跟後台互動,閃的太厲害,不好美觀,因此否決,另覓……

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

方法三:

前台:用個linkbutton控制項

            <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">LinkButton</asp:LinkButton> 

後台:

 protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string s = "";
        for (int i = 0; i <= TreeView1.CheckedNodes.Count - 1; i++)
        {
            s = s + TreeView1.CheckedNodes[i].Text.ToString().Trim() + ",";
        }
        s = s.Remove(s.Length-1);
       
        Response.Write("<script> parent.frames('right').frames('mainframe').location.href  ='MainWindow.aspx?hrz_heng=" + s + "';</script> ");        
    }

最關鍵的代碼是最後一句:

Response.Write("<script> parent.frames('right').frames('mainframe').location.href  ='MainWindow.aspx?hrz_heng=" + s + "';</script> "); 

奶奶的,繞了這麼久,最後原來這麼簡單的方法就能解決……

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.