我在网上down了一个无刷新联动DrowDownList的例子,按照自己的要求进行了一些改动,想实现一下功能:当两个DrowDownList选择相应的地区和县市的值的时候,提交是能把地区和县市的对应的编码显示在label框内,同时将选择的地区和县市的显示的值保持不变;我弄了好久,联动是实现了,可是值就是取不出来,哪位高手过来帮帮小弟我啊!谢谢
代码如下:
Html:
<%@ Page language="c#" Codebehind="TreeDropDownList.aspx.cs" AutoEventWireup="false" Inherits="TowDropDownList.TowDropDownList" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>TreeDropDownList</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<script language="javascript"> var Wantcount;
var Groupcount; Groupcount=0; Group = new Array();
<asp:Literal id="Want" runat="server"></asp:Literal> function changelocation1(locationid)
{
document.submit.City.length = 0;
var locationid=locationid;
var i;
var flag;
var j;
for (i=0;i<Groupcount; i++)
{
if (Group[i][0] == locationid)
{
flag = true;
for (j =0;j<document.submit.City.length;j++)
if (document.submit.City[j].value == Group[i][1])
{
flag = false;
break;
}
if (flag)
document.submit.City.options[document.submit.City.length]
= new Option(Group[i][1], Group[i][1]);
//document.submit.City.Value[document.submit.CityCode.length]=new Option(Group[i][2], Group[i][2]);
}
}
} </script>
</HEAD>
<BODY ms_positioning="GridLayout">
<form id="submit" method="post" runat="server">
<asp:dropdownlist id="Area" style="Z-INDEX: 101; LEFT: 288px; POSITION: absolute; TOP: 72px" runat="server"
Width="100px" onchange="changelocation1(document.submit.Area.options[document.submit.Area.selectedIndex].value);changelocation11(document.submit.Area.options[document.submit.Area.selectedIndex].value,document.submit.City.options[document.submit.City.selectedIndex].value)"
DataValueField="AreaCode" DataTextField="Area"></asp:dropdownlist>
<asp:dropdownlist id="City" style="Z-INDEX: 102; LEFT: 416px; POSITION: absolute; TOP: 72px" runat="server"
Width="100px" DataValueField="CityCode"></asp:dropdownlist>
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 360px; POSITION: absolute; TOP: 168px" runat="server"
Width="120px" Text="Button" Height="24px"></asp:Button>
<asp:Label id="Label1" style="Z-INDEX: 104; LEFT: 288px; POSITION: absolute; TOP: 280px" runat="server"
Width="104px" Height="16px"></asp:Label>
<asp:Label id="Label2" style="Z-INDEX: 105; LEFT: 464px; POSITION: absolute; TOP: 280px" runat="server"
Width="96px" Height="16px"></asp:Label>
<asp:Label id="Label3" style="Z-INDEX: 106; LEFT: 240px; POSITION: absolute; TOP: 280px" runat="server"
Width="48px" Height="24px">县区:</asp:Label>
<asp:Label id="Label4" style="Z-INDEX: 107; LEFT: 408px; POSITION: absolute; TOP: 280px" runat="server"
Width="40px" Height="24px">市区:</asp:Label></form>
<script language="javascript">
changelocation1(document.submit.Area.options[document.submit.Area.selectedIndex].value);
changelocation11(document.submit.Area.options[document.submit.Area.selectedIndex].value,document.submit.City.options[document.submit.City.selectedIndex].value)
</script>
</BODY>
</HTML>
代码如下:
Html:
<%@ Page language="c#" Codebehind="TreeDropDownList.aspx.cs" AutoEventWireup="false" Inherits="TowDropDownList.TowDropDownList" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>TreeDropDownList</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<script language="javascript"> var Wantcount;
var Groupcount; Groupcount=0; Group = new Array();
<asp:Literal id="Want" runat="server"></asp:Literal> function changelocation1(locationid)
{
document.submit.City.length = 0;
var locationid=locationid;
var i;
var flag;
var j;
for (i=0;i<Groupcount; i++)
{
if (Group[i][0] == locationid)
{
flag = true;
for (j =0;j<document.submit.City.length;j++)
if (document.submit.City[j].value == Group[i][1])
{
flag = false;
break;
}
if (flag)
document.submit.City.options[document.submit.City.length]
= new Option(Group[i][1], Group[i][1]);
//document.submit.City.Value[document.submit.CityCode.length]=new Option(Group[i][2], Group[i][2]);
}
}
} </script>
</HEAD>
<BODY ms_positioning="GridLayout">
<form id="submit" method="post" runat="server">
<asp:dropdownlist id="Area" style="Z-INDEX: 101; LEFT: 288px; POSITION: absolute; TOP: 72px" runat="server"
Width="100px" onchange="changelocation1(document.submit.Area.options[document.submit.Area.selectedIndex].value);changelocation11(document.submit.Area.options[document.submit.Area.selectedIndex].value,document.submit.City.options[document.submit.City.selectedIndex].value)"
DataValueField="AreaCode" DataTextField="Area"></asp:dropdownlist>
<asp:dropdownlist id="City" style="Z-INDEX: 102; LEFT: 416px; POSITION: absolute; TOP: 72px" runat="server"
Width="100px" DataValueField="CityCode"></asp:dropdownlist>
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 360px; POSITION: absolute; TOP: 168px" runat="server"
Width="120px" Text="Button" Height="24px"></asp:Button>
<asp:Label id="Label1" style="Z-INDEX: 104; LEFT: 288px; POSITION: absolute; TOP: 280px" runat="server"
Width="104px" Height="16px"></asp:Label>
<asp:Label id="Label2" style="Z-INDEX: 105; LEFT: 464px; POSITION: absolute; TOP: 280px" runat="server"
Width="96px" Height="16px"></asp:Label>
<asp:Label id="Label3" style="Z-INDEX: 106; LEFT: 240px; POSITION: absolute; TOP: 280px" runat="server"
Width="48px" Height="24px">县区:</asp:Label>
<asp:Label id="Label4" style="Z-INDEX: 107; LEFT: 408px; POSITION: absolute; TOP: 280px" runat="server"
Width="40px" Height="24px">市区:</asp:Label></form>
<script language="javascript">
changelocation1(document.submit.Area.options[document.submit.Area.selectedIndex].value);
changelocation11(document.submit.Area.options[document.submit.Area.selectedIndex].value,document.submit.City.options[document.submit.City.selectedIndex].value)
</script>
</BODY>
</HTML>
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; namespace TowDropDownList
{
/// <summary>
/// TowDropDownList 的摘要说明。
/// </summary>
public class TowDropDownList : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Literal Want;
protected System.Web.UI.WebControls.DropDownList City;
protected System.Web.UI.WebControls.DropDownList Area;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Literal Group; private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("DataSource.xml"));
for(int i = 0;i <ds.Tables[0].Rows.Count ;i++)
{
DataRow dr = ds.Tables[0].Rows[i];
Want.Text += String.Format("Group[Groupcount++] = new Array(\"{0}\",\"{1}\",\"{2}\",\"{3}\");\n", dr["Area"],dr["City"].ToString(),dr["AreaCode"].ToString(), dr["CityCode"].ToString());
if (Area.Items.FindByText(dr["Area"].ToString()) == null)
{
Area.Items.Add(dr["Area"].ToString());
//Area.Items.Add(dr["AreaCode"].ToString());
City.Items.Add(dr["City"].ToString());
//City.Items.Add(dr["CityCode"].ToString());
// Convert.ToString(City.Items.FindByText(dr["City"]).Value)=ds.Tables[0].Rows[0][i];
}
}
}
} #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)
{
this.Label1.Text=this.Area.SelectedItem.Value.ToString();
this.Label2.Text=this.City.SelectedValue.ToString();
}
}
}
<DataSource>
<Data>
<Area>不限</Area>
<AreaCode></AreaCode>
<City>不限</City>
<CityCode></CityCode>
</Data>
<Data>
<Area>杭州市</Area>
<AreaCode>3301</AreaCode>
<City>不限</City>
<CityCode></CityCode>
</Data>
<Data>
<Area>杭州市</Area>
<AreaCode>3301</AreaCode>
<City>上城区</City>
<CityCode>330102</CityCode>
</Data>
<Data>
<Area>杭州市</Area>
<AreaCode>3301</AreaCode>
<City>下城区</City>
<CityCode>330103</CityCode>
</Data>
<Data>
<Area>杭州市</Area>
<AreaCode>3301</AreaCode>
<City>江干区</City>
<CityCode>330104</CityCode>
</Data>
<Data>
<Area>杭州市</Area>
<AreaCode>3301</AreaCode>
<City>拱墅区</City>
<CityCode>330105</CityCode>
</Data>
<Data>
<Area>杭州市</Area>
<AreaCode>3301</AreaCode>
<City>西湖区</City>
<CityCode>330106</CityCode>
</Data>
<Data>
<Area>杭州市</Area>
<AreaCode>3301</AreaCode>
<City>滨江区</City>
<CityCode>330108</CityCode>
</Data>
<Data>
<Area>杭州市</Area>
<AreaCode>3301</AreaCode>
<City>萧山区</City>
<CityCode>330109</CityCode>
</Data>
<Data>
<Area>杭州市</Area>
<AreaCode>3301</AreaCode>
<City>余杭区</City>
<CityCode>330110</CityCode>
</Data>
<Data>
<Area>杭州市</Area>
<AreaCode>3301</AreaCode>
<City>桐庐县</City>
<CityCode>330122</CityCode>
</Data>
<Data>
<Area>杭州市</Area>
<AreaCode>3301</AreaCode>
<City>淳安县</City>
<CityCode>330127</CityCode>
</Data>
<Data>
<Area>杭州市</Area>
<AreaCode>3301</AreaCode>
<City>富阳市</City>
<CityCode>330182</CityCode>
</Data>
<Data>
<Area>杭州市</Area>
<AreaCode>3301</AreaCode>
<City>临安市</City>
<CityCode>330185</CityCode>
</Data>
<Data>
<Area>宁波市</Area>
<AreaCode>3302</AreaCode>
<City>不限</City>
<CityCode></CityCode>
</Data>
<Data>
<Area>宁波市</Area>
<AreaCode>3302</AreaCode>
<City>海曙区</City>
<CityCode>330203</CityCode>
</Data>
<Data>
<Area>宁波市</Area>
<AreaCode>3302</AreaCode>
<City>江东区</City>
<CityCode>330204</CityCode>
</Data>
<Data>
<Area>宁波市</Area>
<AreaCode>3302</AreaCode>
<City>江北区</City>
<CityCode>330205</CityCode>
</Data>
<Data>
<Area>宁波市</Area>
<AreaCode>3302</AreaCode>
<City>北仑区</City>
<CityCode>330206</CityCode>
</Data>
<Data>
<Area>宁波市</Area>
<AreaCode>3302</AreaCode>
<City>镇海区</City>
<CityCode>330211</CityCode>
</Data>
<Data>
<Area>宁波市</Area>
<AreaCode>3302</AreaCode>
<City>鄞州区</City>
<CityCode>330212</CityCode>
</Data>
<Data>
<Area>宁波市</Area>
<AreaCode>3302</AreaCode>
<City>象山县</City>
<CityCode>330225</CityCode>
</Data>
<Data>
<Area>宁波市</Area>
<AreaCode>3302</AreaCode>
<City>宁海县</City>
<CityCode>330226</CityCode>
</Data>
<Data>
<Area>宁波市</Area>
<AreaCode>3302</AreaCode>
<City>余姚市</City>
<CityCode>330281</CityCode>
</Data>
</DataSource
Request.Form["City"].....
<form id="submit" method="post" runat="server" action="TreeDropDownList.aspx">
代码里如此接收:this.Label2.Text=Request.Form["City"].ToString();
document.all.txtbox.value = dropCity.value
我不知道怎么取
我要实现这样的
如果选中杭州市 上城区
label中显示的是3301 330102,这样的!!
你说的那个方法我试过了,label框中是能够取回来的
当时怎么样让dropdownlist中的也不变呢!!!
还有就是我想要显示的不是这样的
我要的是对应的代码!!!
http://singlepine.cnblogs.com/articles/265678.html
那个我看过的!!!
我不知道怎么取
我要实现这样的
如果选中杭州市 上城区
label中显示的是3301 330102,这样的!!-----------------------------------------
你只要把DropDownList的VALUE绑定地址文本而不是代号就可以了。
--------------------------------------------------------当时怎么样让dropdownlist中的也不变呢!!!用XMLHTTP。我可以给你一个示例。
根本没把那些code(AreaCode,CityCode)的值写到dropdownlist的value中啊!!!!只是以文本的形式读取出来了而已
可以参考一下
request.form["下拉框名"]
this.Request["DropDownList2"]的值总是为空
连this.Request["DropDownList1"]的值也为空后来我查看调用该ascx控件的aspx页面的html源文件,
发现两个DropDownList的id并不是DropDownList1和DropDownList2了,于是改用this.Request[this.DropDownList1.ClientID]和this.Request[this.DropDownList2.ClientID]获取用户选择值,依然为空================================
解决方式:
在两个DropDownList傍边又加了一个HtmlInputHidden,其id为myHidden设置为服务器端运行
然后在Page_Load中做如下设置
DropDownList2.Attributes.Add("onBlur", ""+this.myHidden.ClientID+".value=this.options[this.selectedIndex].value");
用this.myHidden.Value便获取到了DropDownList2的用户选择值然后再用this.DropDownList1.SelectedValue 获取DropDownList1的选择值