C#后台实现二级联动?

jerysab 2010-11-01 09:39:26
C#后台实现二级联动,用2个dropdownlist,最好是无刷新!如何实现呢?代码具体如何写呢?请高手帮忙指教下!!非常感谢!!!!!!!!
...全文
298 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
自然框架 2011-02-25
  • 打赏
  • 举报
回复
这里有封装好的控件,n级联动无刷新,有源码。有省市区县的数据库
http://topic.csdn.net/u/20110225/16/1aae774d-e92e-4d1f-9065-61b712063c28.html?44405
herott632482577 2010-11-02
  • 打赏
  • 举报
回复
JS满多的网上搜下吧~~~
annywudy_tt 2010-11-02
  • 打赏
  • 举报
回复
用JS做个AXJX不就行了!
tyi001 2010-11-02
  • 打赏
  • 举报
回复
网上找一个很简单 ·
porschev 2010-11-02
  • 打赏
  • 举报
回复
http://blog.csdn.net/porschev/archive/2010/10/15/5943579.aspx

源码里面有下载。。在CSDN上写博客时标签上总会多个<mce:
zhgroup 2010-11-02
  • 打赏
  • 举报
回复
客户端通过AJAX调用服务端,服务端以JSON格式返回数据,如果数据较少,可以一次加载
至于客户端显示,用JS控制吧
donglin0316 2010-11-02
  • 打赏
  • 举报
回复
想无刷可以加个ajax控件
donglin0316 2010-11-02
  • 打赏
  • 举报
回复
省份城市关联
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Provincclist();

}


}
public void Provincclist()
{
SqlConnection conn = opendatabase.connectdb();
conn.Open();
string sql = "select * from S_Province";
SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
DataSet ds = new DataSet();
sda.Fill(ds);
ComProvince.DataSource = ds;
ComProvince.DataTextField = "ProvinceName";
ComProvince.DataValueField = "ProvinceID";
ComProvince.DataBind();
conn.Close();

}
public void DistructList()
{
SqlConnection conn = opendatabase.connectdb();
conn.Open();
string sql = "select * from S_District where CityID = "+ ComCity.SelectedValue;
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
sda.Fill(ds);
ComDistrict.DataSource = ds;
ComDistrict.DataTextField = "DistrictName";
ComDistrict.DataValueField = "DistrictID";
ComDistrict.DataBind();
conn.Close();
ComDistrict.Visible = true;
}
//这里定义了两个函数,作用就是绑定数据的。主要是下面一个方法。
protected void ComProvince_SelectedIndexChanged(object sender, EventArgs e)
{
CityList();
}
cjh200102 2010-11-02
  • 打赏
  • 举报
回复
楼主如果要整个运行代码,给我留言
cjh200102 2010-11-02
  • 打赏
  • 举报
回复
ASHX 文件代码:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");

List<SBDY_DEVICETYPE> bo_list = GetAreaCityList();
if (bo_list.Count > 0)
context.Response.Write(ConvertAreaCityListToJson2(bo_list));
}

private List<SBDY_DEVICETYPE> GetAreaCityList()
{
List<SBDY_DEVICETYPE> bo_list = new List<SBDY_DEVICETYPE>();

object oType = HttpContext.Current.Request["t"];
object oValue = HttpContext.Current.Request["v"];
if (oType == null) // || oValue == null
return bo_list;
else
{
string sType = oType.ToString().Trim();
string sValue = string.Empty; //HttpContext.Current.Server.UrlDecode(oValue.ToString().Trim());

if (oValue != null)
{
sValue = HttpContext.Current.Server.UrlDecode(oValue.ToString().Trim());
}

if (oValue.ToString() != "-1")
{

DeviceTypeBiz dBiz=new DeviceTypeBiz();
if (sType.CompareTo("c") == 0)
{
bo_list = dBiz.SBDY_DEVICETYPE_DropDownList(sValue);
}
}
//else if (sType.CompareTo("n") == 0) //也可用名称级联 如:省份、城市名称
// bo_list = bll.GetAreaCityListByParentName(sValue);
return bo_list;
}
}

private string ConvertAreaCityListToJson2(List<SBDY_DEVICETYPE> bo_list)
{
StringBuilder sb = new StringBuilder();

if (bo_list.Count > 0)
sb.Append("[");
int i = 0;

foreach (SBDY_DEVICETYPE item in bo_list)
{
string cc = "{";
cc += string.Format("code:\"{0}\",name:\"{1}\"", item.ID, item.NAME);
cc += "}"; //todo:去除最后一个逗号
if ((++i) < bo_list.Count)
cc += ",";
sb.Append(cc);
}

if (bo_list.Count > 0)
sb.Append("]");
return sb.ToString();
}


public bool IsReusable
{
get
{
return false;
}
}

JS 代码:
jQuery(document).ready(function() {
jQuery.getJSON(
"DeviceHandler.ashx",
{ t: "c", v: "0" }, //v:"0" 第一个下拉框绑定 一级目录 数据
function(json) {
jQuery.each(json, function(i) {
jQuery("#Select1").append("<option value=\"" + json[i].code + "\">" + json[i].name + "</option>");
});
}
);
});

function unionProvince() {
var index = document.getElementById("Select1").selectedIndex
var code = document.getElementById("Select1").options[index].value;
var name = document.getElementById("Select1").options[index].text;
//清空
jQuery("#Select2").empty();
jQuery("#Select3").empty();
//alert(code + " - " + name);
jQuery("#hidDEVICETYPE1").val(code);
jQuery("#hidDEVICETYPE2").val(""); //清除 二级设备值
jQuery("#hidDEVICETYPE3").val(""); //清除 三级设备值

jQuery("#Select2").append("<option value=\"-1\">请选择...</option>");
jQuery("#Select3").append("<option value=\"-1\">请选择...</option>");

jQuery.getJSON(
"DeviceHandler.ashx",
{ t: "c", v: code },
function(json) {
jQuery.each(json, function(i) {
jQuery("#Select2").append("<option value=\"" + json[i].code + "\">" + json[i].name + "</option>");
});
}
);
//
}

function unionCity() {
var index = document.getElementById("Select2").selectedIndex
var code = document.getElementById("Select2").options[index].value;
var name = document.getElementById("Select2").options[index].text;
//清空
jQuery("#Select3").empty();
//alert(code + " - " + name);
jQuery("#hidDEVICETYPE2").val(code);
jQuery("#hidDEVICETYPE3").val(""); //清除 三级设备值

jQuery("#Select3").append("<option value=\"-1\">请选择...</option>");

jQuery.getJSON(
"DeviceHandler.ashx",
{ t: "c", v: code },
function(json) {
jQuery.each(json, function(i) {
jQuery("#Select3").append("<option value=\"" + json[i].code + "\">" + json[i].name + "</option>");
});
}
);
//
}

function unionArea() {
var index = document.getElementById("Select3").selectedIndex
var code = document.getElementById("Select3").options[index].value;
var name = document.getElementById("Select3").options[index].text;

// alert(code + " - " + name);
jQuery("#hidDEVICETYPE3").val(code);

}
cjh200102 2010-11-02
  • 打赏
  • 举报
回复
用JSON+ASHX来实现
wwtaono1 2010-11-02
  • 打赏
  • 举报
回复
以前用过这个功能,来学习学习
jerysab 2010-11-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 cjh200102 的回复:]
楼主如果要整个运行代码,给我留言
[/Quote]
如何联系你呢? 加我Q502678623
jerysab 2010-11-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 csrwgs 的回复:]
WINFORM /WEBFORM ?

数据量小的话可以先都加载
通过显示/隐藏来实现
[/Quote]

webfrom 显示和隐藏?如何实现呢?
csrwgs 2010-11-01
  • 打赏
  • 举报
回复
WINFORM /WEBFORM ?

数据量小的话可以先都加载
通过显示/隐藏来实现

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧