我有一个stock的数据库(sql server),有2个表,一个是table1,记录各个省名的信息,一个是table2,记录的是各个城市名的信息。例如:
table1:(一个字段:省名) table2:(二个字段:湖北,湖南)
湖北 武汉 长沙
湖南 荆州 邵阳dropdownlist1和table1绑定了,而我希望dropdownlist2和table2是动态绑定,即dropdownlist1中选择了湖北后,dropdownlist2中就显示的是table2中的湖北列,如果dropdownlist1中选择了湖南后,dropdownlist2中就显示的是table2中的湖南列,那么dropdownlist2绑定数据源sqlsource该怎么配置呢?或者用什么办法能达到这个动态的效果呢?
table1:(一个字段:省名) table2:(二个字段:湖北,湖南)
湖北 武汉 长沙
湖南 荆州 邵阳dropdownlist1和table1绑定了,而我希望dropdownlist2和table2是动态绑定,即dropdownlist1中选择了湖北后,dropdownlist2中就显示的是table2中的湖北列,如果dropdownlist1中选择了湖南后,dropdownlist2中就显示的是table2中的湖南列,那么dropdownlist2绑定数据源sqlsource该怎么配置呢?或者用什么办法能达到这个动态的效果呢?
服务器端的和客户端的都可以。
客户端要用AJAX动态读取,然后绑定。
ddlLB.DataValueField = "字段2";
ddlLB.DataSource = ds;
ddlLB.DataBind();
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlLB" runat="server" Width="15%" AutoPostBack="True" OnSelectedIndexChanged="ddlLB_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="ddlChild" runat="server" Width="20%">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
protected void ddlLB_SelectedIndexChanged(object sender, EventArgs e)
{
if(this.ddlLB.SelectedValue!=null)
{
BindChild(this.ddlLB.SelectedValue);
}
}
代码如下:
1,新建js文件,代码如下://设置市,选项function Setcity(ary)
{ //将获取的存储市名称及编码的字符串进行分离,//每个数组元素中存储一个市的名称
var arycity = new Array();
arycity = ary.toString().split("|");
// var list = document.getElementById("ddlcity");
// list.options.length = 0;//清空ddlcity选项
$("#ddlcity").empty();
for(i=0;i<arycity.length-1;i++)
{
var newoption = document.createElement("OPTION");
newoption.text = arycity[i];
document.getElementById("ddlcity").options.add(newoption);
}
}//jquery异步调用City.ashx
$(document).ready(function(){$("#ddlProvince").change(function(){//参数传递的两种方法//1,var code = "procode=" + $("#ddlProvince").val();
//2,data:{procode:code},
//var code = "procode=" + $("#ddlProvince").val();
var code =$("#ddlProvince").val();
$.ajax({
type:"POST",
url:"City.ashx",
data:{procode:code},
success:Setcity
})
})})2,新建ashx文件,代码如下:public class City : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";//接收参数
string code = context.Request.Params["procode"].ToString().Trim();
//string strcity = GetCity(code);
//context.Response.Write("Hello World");
string stritem = GetCity(code);
context.Response.Write(stritem);
} public string GetCity(string procode)
{
List<YD.Model.City> citylist = new YD.BLL.City().GetList("*", " provinceId='"+procode+"'","name asc");
int length = citylist.Count;
string strary = "";
for (int i = 0; i < length; i++)
{
// strary[i] = citylist[i].name + "|" + citylist[i].code;
strary += citylist[i].name +"|";
}
return strary;
}
public bool IsReusable {
get {
return false;
}
}}前台代码://记得引用jquery包<script src="../js/jquery-1.2.6.pack.js" type="text/javascript"></script><script src="Js/getCity.js" type="text/javascript"></script> <td>
<asp:DropDownList ID="ddlProvince" runat="server" CssClass="normal_sele">
</asp:DropDownList>
<asp:DropDownList ID="ddlcity" runat="server" CssClass="normal_sele">
</asp:DropDownList>
</td>
执行以上的sql语句,保存在一个数据源中如dataset1
this.dropdownlist2.textfield = "cityName";
this.dropdownlist2.valuefield = "ID";
this.dropdownlist2.datasource = dataset1;
this.dropdownlist2.databind();
记得将dropdownlist1.autopostback设为true
本人初学ASP.NET,看程序有点吃力,希望大家不要见怪,我在dropdownlist的属性框里找了下,发现datasourceID,这个和2楼说的ddlLB.DataSource = ds;是一个概念吗?
另外BindChild(this.ddlLB.SelectedValue);
这句话该如何理解呢?
我在help里面没有搜到BindChild这个函数。。
5楼的朋友的那句sql命令一直被提示错误,很无奈。。
1、引用Ajax.dll到Bin目录
2、项目根目录Web.config文件增加如下节点<system.web>
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax"/>
</httpHandlers>
</system.web>3、后台代码
using Ajax;protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(后台类名)); 父级下拉框ID.Attributes.Add("onchange", "前台JS方法名");
}先绑定父级下拉框数据;
然后写AJax方法
#region Ajax方法获取子级下拉列表
[AjaxMethod]
public static string GetChildCompanyByParentID(int parentId)
{
string tmp = "";
Service service = new Service();
DepDO dep = new DepDO();
dep.Parent = parentId;
DataSet ds_Area = service.SelectDepByParentID(dep);
if (ds_Area != null && ds_Area.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds_Area.Tables[0].Rows.Count; i++)
{
tmp = tmp + ds_Area.Tables[0].Rows[i]["DepName"].ToString() + "|" + ds_Area.Tables[0].Rows[i]["ID"] + ",";
}
}
tmp = tmp.TrimEnd(',');
return tmp;
}
#endregion
4、前台JS方法
<script type="text/javascript">
function sp()
{
var sp = document.getElementById("ddlParent");
var p = sp.options[sp.selectedIndex].value;
if(p != 1)
{
ReportDetail.GetChildCompanyByParentID(p,callback_GetChildCompanyByParentID);
}
}
function callback_GetChildCompanyByParentID(res)
{
var sc = document.getElementById("ddlChild");
if(res!=null)
{
sc.length = 0;
sc.options.add(new Option("请选择省分公司",-100));
var city = new Array();
city = res.value.split(",");
for(var i=0;i<city.length;i++)
{
var cvalue = new Array();
cvalue = city[i].split("|");
sc.options.add(new Option(cvalue[0],cvalue[1]));
}
}
}
</script>思路和逻辑就是这样的,根据自己的情况该下
DataSourceID与DataSource 是两个不同的概念
DataSourceID指向的是一个数据源控件,如SqlDataSource
DataSource指向的是一个数据,如DataSet,DataTable,List等等
楼主加油!!!