求助求助无限分级的问题

flybisu 2009-08-19 08:18:59
数据库现在有一张表
id name parentid
1 java 0
2 .net 0
3 j2ee 1
4 j2me 1
5 asp.net 2
6 asp.net MVC 5

在页面用下来展示出来1级和2级和3级的下拉连动

不会递归弄这个啊
高手帮下
...全文
64 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
shenjun007 2009-08-19
  • 打赏
  • 举报
回复
一个递归应该就可以了
第一级别循环 显示parentid =0 的
然后 再在这个循环中 显示parentid =id的 就能无限级的显示了
flybisu 2009-08-19
  • 打赏
  • 举报
回复
三级分类还好
关键是用户可以自定义分类 进行无线分级 我就迷茫了撒
wuyq11 2009-08-19
  • 打赏
  • 举报
回复
通过ajax实现三级联动
现获取parentid 为0的数据集,再选择通过事件绑定数据
参考

public class DDLDepartment : DropDownList
{
private bool _blank;
public DDLDepartment()
{
bind(this, 0);
this.Items.Insert(0, new ListItem("==请选择==", ""));
}

public void bind(DropDownList ddlDepartment, int parent)
{
IList<Department> deptlist = DepartmentBLL.SelectChild(parent);
foreach (Department dept in deptlist)
{
string text = new string(' ', dept.Depth - 1);
text += "└" + dept.DeptName;
ddlDepartment.Items.Add(new ListItem(text, dept.DeptId.ToString()));
bind(this, dept.DeptNo);
}
}

public bool Blank
{
set
{
_blank = value;
if (value == false) this.Items.RemoveAt(0);
}
get
{
return _blank;
}
}
}
flybisu 2009-08-19
  • 打赏
  • 举报
回复
谢谢帅哥 我去研究下
zzxap 2009-08-19
  • 打赏
  • 举报
回复
<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();
}
zzxap 2009-08-19
  • 打赏
  • 举报
回复
http://www.cnblogs.com/nirvanalst/archive/2009/01/19/1378190.html
zzxap 2009-08-19
  • 打赏
  • 举报
回复
1.asp.net建立一个AjaxControlTookit web site项目。

2.拖一个UpdatePanel,并设置id="panel1";

3.拖一个Dropdownlist(或是ListBox)到panel1中,并设置id="List2",及AutoPostBanck=True;该panel中的下拉菜单为二级菜单。

4.拖一个Dropdownlist(或是ListBox)到panel1中或是外面都行,并设置id="List1",该下拉菜单为一级菜单。

5.设置ScriptManager1的EnablePartialRendering=True;

6.Page_load添加一级菜单内容。(主要代码)

sqlDataAdapter oda= new sqlDataAdapter ();

oda.SelectCommand= new SqlCommand("sql语句",conn)

DataSet ds= new DataSet();

oda.Fill(ds,"yjcd");

List1.DataSource=ds.Tables["yjcd"];

List1.DataTextField="绑定的数据库字段名"

Lst1.DataBind();

7.一级下拉菜单事件。(主要代码)

List1_selectIndexChanged(Objec source,EnventArgs e)

{

String fenlei=List1.SelectedValue.ToString();

oda.SelectCommand=new sqlCommand("sql语句",conn);

//绑定数据代码同上

}

9.最后UpdataPanel,panel1中的 Triggers,添加AutoPostBack

ControlID=List1;EnventName=List1_SelectedIndexChanged

10.添加一级下拉菜单List1属性AppendDataBoundItems=True;AutoPostBack=True;
flybisu 2009-08-19
  • 打赏
  • 举报
回复
没人帮忙吗
flybisu 2009-08-19
  • 打赏
  • 举报
回复
自己顶一个

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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