第一次用了updatepannel控件来实现 速度有点满,一直奇怪,以前用updatepannel我做的省市联动,上千条数据,速度很快的,可这次却很慢
第二次用了js,xmlrequest,将满足条件的数据生成数组在前台显示。可是依然没有加快速度。
请问还有更好的方法吗?如何实现像在本地数据库一样,速度如飞一样?
5 个解决方案
#1
800条,按照正规的编程方法,速度应该不会达到秒级吧
楼主的实现方式是怎样的,编码可以贴上来看看
楼主的实现方式是怎样的,编码可以贴上来看看
#2
<asp:DropDownList ID="drp1" runat="server" ></asp:DropDownList>
<select id="drp2" onchange="GetBottomDate(this);" ></select>
<select id="drp3" onchange="GetPtId(this);"></select>
/求取middle的数据
function GetMiddleDate(obj)
{
var svalue = obj.value;
var webFileUrl = "?TopID=" + svalue;
var result = "";
var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
xmlHttp.open("POST", webFileUrl, false);
xmlHttp.send(null);
result = xmlHttp.responseText;
if(result != "")
{
var lbx=document.getElementById("drp2");
lbx.style.display="inline-block";
lbx.length=0;
var piArray = result.split(",");
for(var i=0;i<piArray.length;i++)
{
var ary1 = piArray[i].toString().split("|");
document.all("drp2").options.add(new Option(ary1[1].toString(),ary1[0].toString()));
}
}
}
Page_Load中
if (TopID != 0)
{
MiddleBind(TopID);
}
if (MiddleID != 0)
{
MiddleBind(MiddleID);
}
protected void MiddleBind(Int64 id)
{
ProtypeInfoActive protypeinfo_active = new ProtypeInfoActive();
DataTable dt = null;
String myStr = "";
try
{
dt = protypeinfo_active.UA_ProtypeInfo_GetProtyeByParentID(id);
}
catch (Exception ex)
{
PF.SaveToDebug(ex.Message.Trim(), "MiddleBind", Page);
}
if (dt.Rows.Count != 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
myStr += "," + dt.Rows[i][0].ToString() + "|" + dt.Rows[i][1].ToString();
}
myStr = myStr.Substring(1);
}
Response.Write(myStr);
Response.End();
}
#3
drp1.Attributes.Add("onchange", "GetMiddleDate(this);");
#4
使用Anthem控件试试.
前台:
CS文件:
App_Code里面的cs文件:
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register TagPrefix="anthem" Assembly="Anthem" Namespace="Anthem" %><!--添加该行 -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>利用Anthem组件进行三级联动</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<Anthem:DropDownList ID="DropDownList1" runat="server" AutoCallBack="true" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</Anthem:DropDownList>
<Anthem:DropDownList ID="DropDownList2" runat="server" AutoCallBack="true"
EnabledDuringCallBack="false"
onselectedindexchanged="DropDownList2_SelectedIndexChanged">
</Anthem:DropDownList>
<Anthem:DropDownList ID="DropDownList3" runat="server" AutoCallBack="true" EnabledDuringCallBack="false">
</Anthem:DropDownList>
</div>
</form>
</body>
</html>
CS文件:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SQL sql = new SQL();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListItem list = new ListItem();
list.Text = "--请选择--";
list.Value = "";
//进行数据绑定
DataTable da = new DataTable();
da = sql.SelectProvince();//查询数据
DropDownList1.DataSource = da;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "Code";
DropDownList1.DataBind();
//在第一个位置添加"--请选择--"
DropDownList1.Items.Insert(0, list);
DropDownList2.Items.Insert(0, list);
DropDownList3.Items.Insert(0, list);
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ListItem list = new ListItem();
list.Text = "--请选择--";
list.Value = "";
//如果DropDownList1的value值为空,代表没有选择任何的省份.
if (DropDownList1.SelectedValue != "")
{
DataTable dt = new DataTable();
dt = sql.SelectCity(DropDownList1.SelectedValue);//根据DropDownList1的value值(也就是绑定的code)去数据库里面查询属于该code的值
DropDownList2.DataSource = dt;
DropDownList2.DataTextField = "name";
DropDownList2.DataValueField = "code";
DropDownList2.DataBind();
DropDownList2.UpdateAfterCallBack = true;//刷新DropDownList2,这是Anthem组件里面控件的一个属性,单独刷新该控件.
DropDownList2.Items.Insert(0, list);//为第一个位置添加--请选择--
}
else
{
DropDownList2.Items.Clear();//清除DropDownList2里面的值.
DropDownList2.Items.Insert(0, list);
DropDownList2.UpdateAfterCallBack = true;
DropDownList3.Items.Clear();
DropDownList3.Items.Insert(0, list);
DropDownList3.UpdateAfterCallBack = true;
}
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
ListItem list = new ListItem();
list.Text = "--请选择--";
list.Value = "";
if (DropDownList2.SelectedValue != "")
{
DataTable dt = new DataTable();
dt = sql.SelectArea(DropDownList2.SelectedValue);
DropDownList3.DataSource = dt;
DropDownList3.DataTextField = "name";
DropDownList3.DataValueField = "code";
DropDownList3.DataBind();
DropDownList3.UpdateAfterCallBack = true;
DropDownList3.Items.Insert(0, list);
}
else
{
DropDownList3.Items.Clear();
DropDownList3.Items.Insert(0, list);
DropDownList3.UpdateAfterCallBack = true;
}
}
}
App_Code里面的cs文件:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
/// <summary>
/// Summary description for SQL
/// </summary>
public class SQL
{
SqlConnection conn = null;
SqlCommand cmd = null;
String SqlString = ConfigurationManager.AppSettings["City"].ToString();//在配置文件(web.config)里面将SQL的连接字符串写好.
/*********************************************************
* <appSettings>
* <add key="City" value="server=.;user id=sa;password=helloworld;database=my51aspx"/>
* </appSettings>
* 在配置文件web.config添加的SQL连接语句
* *******************************************************/
public SQL()
{
conn = new SqlConnection(SqlString);
}
//查询身份
public DataTable SelectProvince()
{
string sql = "select [Code],[Name] from province";
cmd = new SqlCommand(sql, conn);
SqlDataAdapter sdr = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sdr.Fill(dt);
return dt;
}
//查询城市
public DataTable SelectCity(String provinceid)
{
string sql = "select [Code],[Name] from City where provinceid='" + provinceid + "'";//根据前台传递的参数作为条件去查询.
cmd = new SqlCommand(sql, conn);
SqlDataAdapter sdr = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sdr.Fill(dt);
return dt;
}
//查询区域
public DataTable SelectArea(String Cityid)
{
string sql = "select [Code],[Name] from area where Cityid='" + Cityid + "'";//同上
cmd = new SqlCommand(sql, conn);
SqlDataAdapter sdr = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sdr.Fill(dt);
return dt;
}
}
#5
服务端返回数据源格式的问题吧
#1
800条,按照正规的编程方法,速度应该不会达到秒级吧
楼主的实现方式是怎样的,编码可以贴上来看看
楼主的实现方式是怎样的,编码可以贴上来看看
#2
<asp:DropDownList ID="drp1" runat="server" ></asp:DropDownList>
<select id="drp2" onchange="GetBottomDate(this);" ></select>
<select id="drp3" onchange="GetPtId(this);"></select>
/求取middle的数据
function GetMiddleDate(obj)
{
var svalue = obj.value;
var webFileUrl = "?TopID=" + svalue;
var result = "";
var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
xmlHttp.open("POST", webFileUrl, false);
xmlHttp.send(null);
result = xmlHttp.responseText;
if(result != "")
{
var lbx=document.getElementById("drp2");
lbx.style.display="inline-block";
lbx.length=0;
var piArray = result.split(",");
for(var i=0;i<piArray.length;i++)
{
var ary1 = piArray[i].toString().split("|");
document.all("drp2").options.add(new Option(ary1[1].toString(),ary1[0].toString()));
}
}
}
Page_Load中
if (TopID != 0)
{
MiddleBind(TopID);
}
if (MiddleID != 0)
{
MiddleBind(MiddleID);
}
protected void MiddleBind(Int64 id)
{
ProtypeInfoActive protypeinfo_active = new ProtypeInfoActive();
DataTable dt = null;
String myStr = "";
try
{
dt = protypeinfo_active.UA_ProtypeInfo_GetProtyeByParentID(id);
}
catch (Exception ex)
{
PF.SaveToDebug(ex.Message.Trim(), "MiddleBind", Page);
}
if (dt.Rows.Count != 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
myStr += "," + dt.Rows[i][0].ToString() + "|" + dt.Rows[i][1].ToString();
}
myStr = myStr.Substring(1);
}
Response.Write(myStr);
Response.End();
}
#3
drp1.Attributes.Add("onchange", "GetMiddleDate(this);");
#4
使用Anthem控件试试.
前台:
CS文件:
App_Code里面的cs文件:
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register TagPrefix="anthem" Assembly="Anthem" Namespace="Anthem" %><!--添加该行 -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>利用Anthem组件进行三级联动</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<Anthem:DropDownList ID="DropDownList1" runat="server" AutoCallBack="true" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</Anthem:DropDownList>
<Anthem:DropDownList ID="DropDownList2" runat="server" AutoCallBack="true"
EnabledDuringCallBack="false"
onselectedindexchanged="DropDownList2_SelectedIndexChanged">
</Anthem:DropDownList>
<Anthem:DropDownList ID="DropDownList3" runat="server" AutoCallBack="true" EnabledDuringCallBack="false">
</Anthem:DropDownList>
</div>
</form>
</body>
</html>
CS文件:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SQL sql = new SQL();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListItem list = new ListItem();
list.Text = "--请选择--";
list.Value = "";
//进行数据绑定
DataTable da = new DataTable();
da = sql.SelectProvince();//查询数据
DropDownList1.DataSource = da;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "Code";
DropDownList1.DataBind();
//在第一个位置添加"--请选择--"
DropDownList1.Items.Insert(0, list);
DropDownList2.Items.Insert(0, list);
DropDownList3.Items.Insert(0, list);
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ListItem list = new ListItem();
list.Text = "--请选择--";
list.Value = "";
//如果DropDownList1的value值为空,代表没有选择任何的省份.
if (DropDownList1.SelectedValue != "")
{
DataTable dt = new DataTable();
dt = sql.SelectCity(DropDownList1.SelectedValue);//根据DropDownList1的value值(也就是绑定的code)去数据库里面查询属于该code的值
DropDownList2.DataSource = dt;
DropDownList2.DataTextField = "name";
DropDownList2.DataValueField = "code";
DropDownList2.DataBind();
DropDownList2.UpdateAfterCallBack = true;//刷新DropDownList2,这是Anthem组件里面控件的一个属性,单独刷新该控件.
DropDownList2.Items.Insert(0, list);//为第一个位置添加--请选择--
}
else
{
DropDownList2.Items.Clear();//清除DropDownList2里面的值.
DropDownList2.Items.Insert(0, list);
DropDownList2.UpdateAfterCallBack = true;
DropDownList3.Items.Clear();
DropDownList3.Items.Insert(0, list);
DropDownList3.UpdateAfterCallBack = true;
}
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
ListItem list = new ListItem();
list.Text = "--请选择--";
list.Value = "";
if (DropDownList2.SelectedValue != "")
{
DataTable dt = new DataTable();
dt = sql.SelectArea(DropDownList2.SelectedValue);
DropDownList3.DataSource = dt;
DropDownList3.DataTextField = "name";
DropDownList3.DataValueField = "code";
DropDownList3.DataBind();
DropDownList3.UpdateAfterCallBack = true;
DropDownList3.Items.Insert(0, list);
}
else
{
DropDownList3.Items.Clear();
DropDownList3.Items.Insert(0, list);
DropDownList3.UpdateAfterCallBack = true;
}
}
}
App_Code里面的cs文件:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
/// <summary>
/// Summary description for SQL
/// </summary>
public class SQL
{
SqlConnection conn = null;
SqlCommand cmd = null;
String SqlString = ConfigurationManager.AppSettings["City"].ToString();//在配置文件(web.config)里面将SQL的连接字符串写好.
/*********************************************************
* <appSettings>
* <add key="City" value="server=.;user id=sa;password=helloworld;database=my51aspx"/>
* </appSettings>
* 在配置文件web.config添加的SQL连接语句
* *******************************************************/
public SQL()
{
conn = new SqlConnection(SqlString);
}
//查询身份
public DataTable SelectProvince()
{
string sql = "select [Code],[Name] from province";
cmd = new SqlCommand(sql, conn);
SqlDataAdapter sdr = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sdr.Fill(dt);
return dt;
}
//查询城市
public DataTable SelectCity(String provinceid)
{
string sql = "select [Code],[Name] from City where provinceid='" + provinceid + "'";//根据前台传递的参数作为条件去查询.
cmd = new SqlCommand(sql, conn);
SqlDataAdapter sdr = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sdr.Fill(dt);
return dt;
}
//查询区域
public DataTable SelectArea(String Cityid)
{
string sql = "select [Code],[Name] from area where Cityid='" + Cityid + "'";//同上
cmd = new SqlCommand(sql, conn);
SqlDataAdapter sdr = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sdr.Fill(dt);
return dt;
}
}
#5
服务端返回数据源格式的问题吧