对asp.net实现DropDownList、ListBox无刷新三级联动(javascript)的改进
2007-09-14 00:05
856 查看
上篇文章说明了如何实现三级联动,但如果数据量较大,加载时会慢一些,因此经过测试,将需要读取的数据先写入一个文本文件中,然后在加载页面时直接调用此文本文件的内容,省去了读取数据库浪费的时间,数据量大时速度比先前快了许多。
数据库请见上一篇文章“asp.net实现DropDownList、ListBox无刷新三级联动(javascript)”
生成数据文本文件的代码
//将地区信息写入文本文件area.js
//添加二级地区
string strAreaData, strSQL;
strAreaData = "<script language = 'JavaScript' type='text/javascript'>var onecount;onecount=0;subcat = new Array();";
strSQL = "select * from nts_area where oneid<>0 and twoid<>0 and threeid=0 order by sort";
IDataReader dr = DataClass.GetDataReader(strSQL);
int i1 = 0;
while (dr.Read())
strAreaData += "onecount=" + i1.ToString() + ";";
//添加三级地区
strAreaData += "var onecount1;onecount1=0;subcat1 = new Array();";
strSQL = "select * from nts_area where oneid<>0 and twoid<>0 and threeid<>0 order by sort";
dr = DataClass.GetDataReader(strSQL);
int n1 = 0;
while (dr.Read())
dr.Close();
strAreaData += "onecount1=" + n1.ToString() + ";</script>";
StreamWriter str;
File.Delete(Server.MapPath("~/js/area.js"));
str = File.CreateText(Server.MapPath("~/js/area.js"));
str.WriteLine(strAreaData);
str.Close();
aspx页面代码
<%--地区无刷新显示 --%>
<% Response.WriteFile(Server.MapPath("~/js/area.js")); %>
<script language = "JavaScript" type="text/javascript">
function changearea1()
function changearea2()
</script>
<asp:DropDownList ID="Ddl_AreaOne" runat="server" AppendDataBoundItems="True">
<asp:ListItem Value="0">请选择</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="Ddl_AreaTwo" runat="server" >
<asp:ListItem Value="0">请选择</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="Ddl_AreaThree" runat="server" >
<asp:ListItem Value="0">请选择</asp:ListItem>
</asp:DropDownList>
cs页面代码
protected void Page_Load(object sender, EventArgs e)
{
Ddl_AreaOne.Attributes.Add("onchange", "changearea1()");
Ddl_AreaTwo.Attributes.Add("onchange", "changearea2()");
string strSQL;
strSQL = "select * from nts_area where oneid<>0 and twoid=0 and threeid=0 order by sort";
Ddl_AreaOne.DataSource = DataClass.GetDataReader(strSQL); //此为我写的读取DataReader对象的类
Ddl_AreaOne.DataTextField = "areaname";
Ddl_AreaOne.DataValueField = "oneid";
Ddl_AreaOne.DataBind();
}
数据库请见上一篇文章“asp.net实现DropDownList、ListBox无刷新三级联动(javascript)”
生成数据文本文件的代码
//将地区信息写入文本文件area.js
//添加二级地区
string strAreaData, strSQL;
strAreaData = "<script language = 'JavaScript' type='text/javascript'>var onecount;onecount=0;subcat = new Array();";
strSQL = "select * from nts_area where oneid<>0 and twoid<>0 and threeid=0 order by sort";
IDataReader dr = DataClass.GetDataReader(strSQL);
int i1 = 0;
while (dr.Read())
strAreaData += "onecount=" + i1.ToString() + ";";
//添加三级地区
strAreaData += "var onecount1;onecount1=0;subcat1 = new Array();";
strSQL = "select * from nts_area where oneid<>0 and twoid<>0 and threeid<>0 order by sort";
dr = DataClass.GetDataReader(strSQL);
int n1 = 0;
while (dr.Read())
dr.Close();
strAreaData += "onecount1=" + n1.ToString() + ";</script>";
StreamWriter str;
File.Delete(Server.MapPath("~/js/area.js"));
str = File.CreateText(Server.MapPath("~/js/area.js"));
str.WriteLine(strAreaData);
str.Close();
aspx页面代码
<%--地区无刷新显示 --%>
<% Response.WriteFile(Server.MapPath("~/js/area.js")); %>
<script language = "JavaScript" type="text/javascript">
function changearea1()
function changearea2()
</script>
<asp:DropDownList ID="Ddl_AreaOne" runat="server" AppendDataBoundItems="True">
<asp:ListItem Value="0">请选择</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="Ddl_AreaTwo" runat="server" >
<asp:ListItem Value="0">请选择</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="Ddl_AreaThree" runat="server" >
<asp:ListItem Value="0">请选择</asp:ListItem>
</asp:DropDownList>
cs页面代码
protected void Page_Load(object sender, EventArgs e)
{
Ddl_AreaOne.Attributes.Add("onchange", "changearea1()");
Ddl_AreaTwo.Attributes.Add("onchange", "changearea2()");
string strSQL;
strSQL = "select * from nts_area where oneid<>0 and twoid=0 and threeid=0 order by sort";
Ddl_AreaOne.DataSource = DataClass.GetDataReader(strSQL); //此为我写的读取DataReader对象的类
Ddl_AreaOne.DataTextField = "areaname";
Ddl_AreaOne.DataValueField = "oneid";
Ddl_AreaOne.DataBind();
}
相关文章推荐
- 对asp.net实现DropDownList、ListBox无刷新三级联动(javascript)修改
- asp.net实现DropDownList、ListBox无刷新三级联动(javascript)
- asp.net实现DropDownList、ListBox无刷新三级联动(javascript)
- asp.net实现DropDownList、ListBox无刷新三级联动的方法
- asp.net实现DropDownList、ListBox无刷新三级联动的两种方法
- asp.net实现ListBox、DropDownList无刷新三级联动(xmlhttp)
- asp.net实现ListBox、DropDownList无刷新三级联动(xmlhttp)
- asp.net实现DropDownList、ListBox无刷新三级联动的两种方法
- ASP.NET利用JavaScript实现无刷新闪烁二级联动菜单
- asp.net DropDownList无刷新ajax二级联动实现详细过程
- asp.net实现无刷新DropDownList联动控件
- Ajax无刷新技术实现省市县三级联动下拉菜单--Asp.Net
- asp.net 有刷新实现三级联动
- ASP.NET中DropDownList和ListBox实现两级联动功能
- ASP.NET中DropDownList和ListBox实现两级联动功能
- 用ASP.NET AJAX实现无刷新多个DropDownList的动态联动
- ASP.NET利用JavaScript实现无刷新闪烁二级联动菜单
- JavaScript实现DropDownList(Select)三级联动无刷新
- asp.net实现无刷新DropDownList联动控件
- Ajax实现无刷新三联动下拉框 VS2005 ASP.NET DropDownList