前段时间,我在利用ajaxAjaxControlToolkit插件时,发现有时它的值不值为何无法取值。 后我写了一个利用AJAX技术写的一个DropDownList联动的;
下面是它的内容:
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server" Style="position: relative">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" onchange= "Change()">
</asp:DropDownList>
<script type="text/javascript" language="javascript">
<!--
function XmlPost(Object)//联动取值绑定DropDownList2。后台实现无刷新绑定操作;
{
var svalue = Object.value;
var webFileUrl = "?ZoneID=" + svalue;
var result = "";
var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
xmlHttp.open("POST", webFileUrl, false);
xmlHttp.send("");
result = xmlHttp.responseText;
if(result != "" && result != "----" )
{
document.all("DropDownList2").length=0;
var piArray = result.split(",");
//分离字符串并绑定数据
for(var i=0;i<piArray.length;i++)
{
var ary1 = piArray[i].toString().split("|");
document.all("DropDownList2").options.add(new Option(ary1[1].toString(),ary1[0].toString()));
document.getElementById("Label1").innerText = document.getElementById("DropDownList2").value;
}
}
else if( result == "----")
{
document.all("DropDownList2").length=0;
document.all("DropDownList2").options.add(new Option("--不限--","--不限--"));
document.getElementById("Label1").innerText = "--不限--";
}
else
{
alert(result);
}
}
function Change()
{
document.getElementById("Label1").innerText = document.getElementById("DropDownList2").value;
}
-->
</script>
<asp:Label ID="Label1" runat="server" Style="position: relative" Text="City"></asp:Label>
</form>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
string brc_id = this.Request.QueryString["ZoneID"];
if (brc_id != null)
{
this.down2_bind(brc_id);
}
else
{
if (!this.IsPostBack)
{
this.down1_bind();
this.DropDownList2.Items.Clear();
this.DropDownList2.Items.Add("--不限--");
this.DropDownList2.SelectedIndex = this.DropDownList2.Items.Count - 1;
}
}
}
private void down1_bind()// 绑定DropDownList1数据
{
DataTable mytab = bll.GetPrivnice(1);
this.DropDownList1.DataSource = mytab;
this.DropDownList1.DataValueField = "ZoneID";
this.DropDownList1.DataTextField = "ZoneName";
this.DropDownList1.DataBind();
this.DropDownList1.Items.Add("--不限--");
this.DropDownList1.SelectedIndex = this.DropDownList1.Items.Count - 1;
this.DropDownList1.Attributes.Add("onchange", "XmlPost(this);");//当DropDownList变动时,联动DropDownList2的值。
}
private void down2_bind(string brc_id)// 取得/绑定DropDownList1数
{
int a = 1;
string mystr = "";
if (brc_id != "" && brc_id != "----")
{//将取出的若干值串成字符串
a = int.Parse(brc_id);
DataTable mytab = bll.GetPrivnice(a);//返回table暂存于内存
if (mytab.Rows.Count != 0)
{
for (int i = 0; i < mytab.Rows.Count; i++)
{
mystr += "," + mytab.Rows[i][1].ToString() + "|" + mytab.Rows[i][2].ToString();
}
mystr = mystr.Substring(1);
}
this.Response.Write(mystr);
this.Response.End();
}
else if (brc_id == "----")//如果为不限时DropDownList2绑定为不限
{
mystr = brc_id;
this.DropDownList2.Items.Clear();
this.DropDownList2.Items.Add("--不限--");
this.DropDownList2.SelectedIndex = this.DropDownList2.Items.Count - 1;
this.Response.Write(mystr);
this.Response.End();
}
嘻嘻!初学AJAX技术。
浙公网安备 33010602011771号