asp.net的UpdatePanel控件

为了提高树形控件的刷新效率,用局部刷新技术。

.aspx代码如下:



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="中国市区县.aspx.cs" Inherits="中国市区县" %>


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


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
    .fl{ float:left;}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   <div class="fl" runat="server">
  <p><%=DateTime.Now.ToString() %></p>
    <asp:TreeView ID="tv_china" runat="server" 
          onselectednodechanged="tv_china_SelectedNodeChanged" ExpandDepth="0"   >
    </asp:TreeView>
    <asp:ScriptManager
            ID="ScriptManager1" runat="server">
        </asp:ScriptManager>

    </div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
        <p><%=DateTime.Now.ToString() %></p>
        TEXT:<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        VALUE:<asp:Label ID="Label2" runat="server" Text=""></asp:Label>
        VALUEPATH:<asp:Label ID="Label3" runat="server" Text=""></asp:Label>
        
        </ContentTemplate>
        <Triggers >
        <asp:AsyncPostBackTrigger ControlID="tv_china"  />
        </Triggers>

        </asp:UpdatePanel>
        
        
    </div>
    
    </form>
</body>
</html>

注意观察两个控件内时间的变化,可以看出局部刷新的效果。

.aspx.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class 中国市区县 : System.Web.UI.Page
{
    string str_cnn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=";
    string str_sourcefile = "~/data/china.mdb";
    OleDbConnection cnn;
    OleDbCommand cmd;
    OleDbDataReader datar;
    string str_sql;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            makeTree();
        }
          
    }
    protected void makeTree() {
        TreeNode _tnode, _parentNode;
        string str_conn = str_cnn + MapPath(str_sourcefile);
        cnn = new OleDbConnection(str_conn);
        cnn.Open();

        //构建一级(省)
        str_sql = "select p_id,p_name from t_province";
        cmd = new OleDbCommand(str_sql, cnn);
        datar = cmd.ExecuteReader();

        while (datar.Read())
        {
            _tnode = new TreeNode();
            _tnode.Text = datar["p_name"].ToString();
            _tnode.Value="p_"+datar["p_id"].ToString();
            tv_china.Nodes.Add(_tnode);
        
        }
        //构建二级(市)
        str_sql = "select c_id,c_name,c_pid from t_city";
        cmd = new OleDbCommand(str_sql, cnn);
        datar = cmd.ExecuteReader();

        while (datar.Read())
        {
            _parentNode = tv_china.FindNode("p_" + datar["c_pid"].ToString());
            _tnode = new TreeNode();

            _tnode.Text = datar["c_name"].ToString();
            _tnode.Value = "c_" + datar["c_id"].ToString();
            _parentNode.ChildNodes.Add(_tnode);

        }

        //构建三级(区)
        str_sql = "SELECT T_City.c_pid, T_City.c_id, T_District.d_id, T_District.d_name FROM T_District INNER JOIN T_City ON T_District.d_cid = T_City.c_id";
        cmd = new OleDbCommand(str_sql, cnn);
        datar = cmd.ExecuteReader();

        while (datar.Read())
        {
            _parentNode = tv_china.FindNode("p_" + datar["c_pid"].ToString() + "/c_" + datar["c_id"].ToString());
            _tnode = new TreeNode();
            _tnode.Text = datar["d_name"].ToString();
            _tnode.Value = "d_" + datar["d_id"].ToString();

            _parentNode.ChildNodes.Add(_tnode);
        }



        cnn.Close();

    
    }

    protected void tv_china_SelectedNodeChanged(object sender, EventArgs e)
    {
        TreeNode _tnode;
        _tnode = tv_china.SelectedNode;

        Label1.Text = _tnode.Text;
        Label2.Text = _tnode.Value;
        Label3.Text = _tnode.ValuePath;
    }
}
效果如下:



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。