在网上看到了一些无刷新的联动控件。下下了测试以后,显示英文和数字可以,但是不可以显示中文,后来把从网上down下来的文件做了一个翻修改,做了一个省市的联动控件。给大家共享一下。可以显示中文了。
<%@ page language=”c#” codebehind=”example.aspx.cs” autoeventwireup=”false” inherits=”webs.other.example” %>
<html>
<head>
<title>example</title>
<meta name=”generator” content=”microsoft visual studio .net 7.1″>
<meta name=”code_language” content=”c#”>
<meta name=”vs_defaultclientscript” content=”javascript”>
<meta name=”vs_targetschema” content=”http://schemas.microsoft.com/intellisense/ie5″>
<script>
function load(state){
var drp2 = document.getelementbyid(“dropdownlist2”);
for (i = drp2.length; i >= 0; i–){
drp2.options.remove(i);
}
var ohttpreq = new activexobject(“msxml2.xmlhttp”);
var odoc = new activexobject(“msxml2.domdocument”);
ohttpreq.open(“post”, “getdata.aspx?state=”+state, false);
ohttpreq.send(“”);
result = ohttpreq.responsetext;
odoc.loadxml(result);
items1 = odoc.selectnodes(“//city/table/id”);
items2 = odoc.selectnodes(“//city/table/shiname”);
var itemslength=items1.length;
for(i=0;i<itemslength;i++)
//将小类的类名和编号赋予dropdownlist2
{
var newoption = document.createelement(“option”);
newoption.text=items2[i].text;
newoption.value=items1[i].text;
drp2.options.add(newoption);
}
}
window.onload = function(){load(1);}
</script>
</head>
<body ms_positioning=”flowlayout”>
<form id=”form1″ method=”post” runat=”server”>
<asp:dropdownlist id=”dropdownlist1″ runat=”server”></asp:dropdownlist>
<asp:dropdownlist id=”dropdownlist2″ runat=”server”></asp:dropdownlist>
<asp:textbox id=”th” runat=”server”></asp:textbox>
<asp:button id=”button1″ runat=”server” text=”button”></asp:button>
</form>
</body>
</html>
cs源文件:
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.data.sqlclient;
namespace webs.other
{
/// <summary>
/// example 的摘要说明。
/// </summary>
public class example : system.web.ui.page
{
protected system.web.ui.webcontrols.dropdownlist dropdownlist1;
protected system.web.ui.webcontrols.textbox th;
protected system.web.ui.webcontrols.button button1;
protected system.web.ui.webcontrols.dropdownlist dropdownlist2;
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
if(!this.ispostback)
{
// 建立数据源加载第一个dropdownlist,也可以默认加载第二个
sqlconnection con = new sqlconnection(system.configuration.configurationsettings.appsettings.get(“connstr1”));
sqldataadapter da = new sqldataadapter(“select id,shengname from province”,con);
dataset ds = new dataset();
da.fill(ds);
this.dropdownlist1.datasource = ds;
this.dropdownlist1.datatextfield = “shengname”;
this.dropdownlist1.datavaluefield = “id”;
this.dropdownlist1.databind();
// 这里是绑定客户端事件,当第一个dropdownlist的选项改变时激发下面的事件onchange,这个事件将调用一个客户端方法load()
this.dropdownlist1.attributes.add(“onchange”,”load(this.options[this.selectedindex].value)”);
}
}
#region web form designer generated code
override protected void oninit(eventargs e)
{
//
// codegen:该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 – 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.button1.click += new system.eventhandler(this.button1_click);
this.load += new system.eventhandler(this.page_load);
}
#endregion
private void button1_click(object sender, system.eventargs e)
{
th.text=this.request.form[“dropdownlist2”].tostring();
}
}
}
getdata源文件
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.data.sqlclient;
using system.xml;
using system.configuration;
using system.text;
namespace webs.other
{
/// <summary>
/// getdata 的摘要说明。
/// </summary>
public class getdata : system.web.ui.page
{
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
//response.write(request[“state”]);
int shengno=int.parse(request[“state”].tostring());
sqlconnection con = new sqlconnection(system.configuration.configurationsettings.appsettings.get(“connstr1”));
sqldataadapter da = new sqldataadapter(“select id,shiname from city where shengid=”+shengno+””,con);
dataset ds = new dataset(“city”);
da.fill(ds);
xmltextwriter writer = new xmltextwriter(response.outputstream,encoding.utf8);
writer.formatting = formatting.indented;
writer.indentation = 4;
writer.indentchar = ;
writer.writestartdocument();
ds.writexml(writer);
writer.flush();
response.end();
writer.close();
}
#region web form designer generated code
override protected void oninit(eventargs e)
{
//
// codegen:该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 – 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
}
#endregion
}
}
