首页 > 代码库 > asp.net的UpdatePanel控件

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=http://www.mamicode.com/"p_"+datar["p_id"].ToString();>效果如下:



asp.net的UpdatePanel控件