欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

asp:TreeView UpdatePanel学习笔记

程序员文章站 2022-03-04 11:49:20
...

        TreeVew控件由Treenode(节点)构成,一个节点主要是将数据存储在两个属性,Text属性和 value属性。 值 Text 属性显示在 TreeView 控件,Value 属性用来存储节点的其他数据,如用于处理回发事件的相关数据。TreeView控件中的节点有两种模式:选择模式和导航模式。默认处于选择模式,若要使其处于导航模式,将NavigateUrl设置为与节点相关联的页面。若设置为空,则处于选择模式。Target属性设置目标内容或框架,用于显示与节点相关联的内容。NavigateUrl设置单击节点时要导航到的页面。

      当节点的属性PopulateOnDemand设置为trues时,在该节点展开运行时动态填充该节点,动态填充节点时调用OnTreeNodePopulate事件方法引发TreeNodePopulate事件进行填充。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<frameset name="total" rows="*" cols="*" border=0 framespacing=0 frameborder="no" bordercolor="#3366cc">
    <frameset cols="200,*" name=ecc frameborder="no" bordercolor="#3366cc">
        <frame src="tree1.aspx" name="leftFrame" scrolling="no" frameborder="no" id="leftFrame" title="leftFrame" />
        <frame src="tabContent.aspx" name="mainFrame" id="mainFrame" frameborder="no" title="mainFrame" scrolling="no" />
    </frameset>
</frameset>
<noframes>
    <body>
    </body>
</noframes>
</html>

home.HTML

<body>
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="treeviewTest1" ForeColor="Black" ShowLines="true" ExpandDepth="0" runat="server" 
            OnTreeNodePopulate="treeviewTest1_TreeNodePopulate" >
            <Nodes>
                <asp:TreeNode Text="ROOT" SelectAction="Expand" PopulateOnDemand="true" Value="0" NavigateUrl="~/tabContent.aspx?uid=0" Target="mainFrame"/>
            </Nodes>
        </asp:TreeView> 
    </div>
    </form>
</body>

tree1.aspx

 public partial class tree1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //动态填充节点事件
        protected void treeviewTest1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            firstLevelAdd(e.Node);
        }

        //填充节点
        private void firstLevelAdd(TreeNode treeNode)
        {
            DataSet dtset = Runquery("select OBJECTID,OBJECTVALUE from objectTable where OBJECTPARENTID=" + treeNode.Value);
            if (dtset.Tables.Count>0)
            {
                foreach (DataRow row in dtset.Tables[0].Rows)
                {
                    string uid = row["OBJECTID"].ToString();
                    TreeNode newtreenode = new TreeNode(row["OBJECTVALUE"].ToString(), row["OBJECTID"].ToString());
                    newtreenode.PopulateOnDemand = true;
                    //newtreenode.SelectAction = TreeNodeSelectAction.Expand;
                    newtreenode.Target = "mainFrame";
                    newtreenode.NavigateUrl = "tabContent.aspx?uid=" + System.Web.HttpUtility.UrlEncode(uid);
                    treeNode.ChildNodes.Add(newtreenode);
                }
            }
        }

        //查询节点信息
        private DataSet Runquery(string querystr)
        {
            string connstr = "Data Source=HEX208;Initial Catalog=treeviewTest;Integrated Security=True";
            SqlConnection sqlconn = new SqlConnection(connstr);
            sqlconn.Open();
            SqlDataAdapter sqldap;
            DataSet dtset=new DataSet();
            try
            {
                sqldap = new SqlDataAdapter(querystr, sqlconn);
                sqldap.Fill(dtset);
                sqlconn.Close();
            }
            catch (Exception ex)
            {
                if(sqlconn.State==ConnectionState.Open)
                {
                    sqlconn.Close();
                }
                Console.WriteLine(ex.Message);
            }
            return dtset;

        }

    }







tree1.aspx.cs

       UpdatePanel控件是局部更新控件,一般与asp:ScriptManager结合使用。包含了ContenTemplate与Triggers两部分。Triggers为触发器(局部跟新触发器),Triggers为空时,UpdatePanel内部控件触发局部更新。Triggers设置AsynPostedBackTrigger时,内/外部控件触发局部控件。Triggers设置PostedBackTrigger时,内/外部控件触发全局更新。

<form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <div>
            <table>
                <tr>
                    <td>
                        <asp:Button id="bt1" runat="server" OnClick="bt1_Click" Text="TSTbt1"/>
                    </td>
                    <td>
                        <asp:TextBox ID="tstTxt2" runat="server"></asp:TextBox>
                    </td>
                </tr>
            </table>
            <asp:UpdatePanel runat="server" ID="updatePanel1">
                <ContentTemplate>
                    <asp:GridView id="tstGView" runat="server" Width="98%" OnSelectedIndexChanged="tstGView_SelectedIndexChanged">
                        <Columns>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    <asp:CheckBox runat="server" id="cbAll" Text="select" OnCheckedChanged="cbAll_CheckedChanged"/>
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="cbSingle" runat="server" Text="select" OnCheckedChanged="cbSingle_CheckedChanged" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <HeaderTemplate>Edit</HeaderTemplate>
                                <ItemTemplate>
                                    <asp:LinkButton runat="server" ID="edit" Text="edit" ></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                    <asp:TextBox ID="tstTxt1" runat="server"></asp:TextBox>
                    <asp:Button ID="bt2" runat="server" OnClick="bt2_Click" Text="TSTbt2"/>
                </ContentTemplate>
                <Triggers>
                    <%--<asp:AsyncPostBackTrigger  ControlID="bt2" EventName="Click"/>
                    <asp:PostBackTrigger ControlID="bt1"/>--%>
                </Triggers>
            </asp:UpdatePanel>
        </div>
    </form>
tabContent.aspx

protected void Page_Load(object sender, EventArgs e)
        {
            initDataBind();
            this.tstTxt1.Text = DateTime.Now.ToString();
            this.tstTxt2.Text = DateTime.Now.ToString();
        }

        
        private void initDataBind()
        {
            string str = "select * from objectTable where OBJECTPARENTID="+getuid();
            DataSet ds=query(str);
            if(ds.Tables[0]!=null)
            {
                DataTable dt=new DataTable();
                DataColumn dc = new DataColumn("Object ID", typeof(string));
                dt.Columns.Add(dc);
                dc = new DataColumn("Object Name", typeof(string));
                dt.Columns.Add(dc);
                dc = new DataColumn("Parent ID", typeof(string));
                dt.Columns.Add(dc);
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = row["OBJECTID"].ToString();
                    dr[1] = row["OBJECTVALUE"].ToString();
                    dr[2] = row["OBJECTPARENTID"].ToString();
                    dt.Rows.Add(dr);
                }
                this.tstGView.DataSource = dt;
                this.tstGView.DataBind();
            }
        }

        private DataSet query(string str)
        {
            string connstr = "Data Source=HEX208;Initial Catalog=treeviewTest;Integrated Security=True";
            SqlConnection sqlconn = new SqlConnection(connstr);
            sqlconn.Open();
            SqlDataAdapter sqldap;
            DataSet dtset = new DataSet();
            try
            {
                sqldap = new SqlDataAdapter(str, sqlconn);
                sqldap.Fill(dtset);
                sqlconn.Close();
            }
            catch (Exception ex)
            {
                if (sqlconn.State == ConnectionState.Open)
                {
                    sqlconn.Close();
                }
                Console.WriteLine(ex.Message);
            }
            return dtset;
        }
        private string getuid()
        {
            string uid = "0";
            try
            {
                string uidquery = Request.QueryString["uid"].ToString();
                if (uidquery != null)
                {
                    uid = uidquery;
                }
            }
            catch
            {
            }
            return uid;
        }
tabContent.aspx.cs
asp:TreeView UpdatePanel学习笔记

相关标签: treeview