怎样用Ajax实现listbox 局部刷新 实现无限级联

crazyminds110 2009-03-25 10:49:13
有几个无限级分类,每个分类的深度可能不一样

功能:想实现一个无限级的listbox级联,页面加载时,显示根类的listbox。点击时,显示选中分类的子类也就是第二个listbox。

以此类推。当选择另一个根listbox时,第二个listbox呈现出来,第三个、第四个隐藏。
怎么样用Ajax 实现 页面无刷新显示??????
...全文
264 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
fengsh1988214 2012-01-18
  • 打赏
  • 举报
回复
listBox 和DropDownList不一样吧? 貌似不能简单的用UpdatePanel 求解答!
helanye 2009-06-02
  • 打赏
  • 举报
回复
我也想知道怎么实现,我也遇到类似的问题,没有解决了。
南哥1207 2009-03-31
  • 打赏
  • 举报
回复
下载ajax控件,如果楼主没有的话,
有的话就同5楼说的

[Quote=引用 5 楼 dk9761 的回复:]
aspx代码
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
</div>
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">

</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:A…
[/Quote]
zzxap 2009-03-31
  • 打赏
  • 举报
回复
[code=C#]
用Ajax.net实现的一个无刷新的多级联动下拉列表框
(转载)
用Ajax.NET Ectention 实现的一个无刷新的多级动态下拉列表框,使用的3个UpdatePanel,每一个中放一个DropDownList,分别为
DropDownList1、2、2,其中UpdatePanel2由UpdatePanel1触发,UpdatePanel3由UpdatePanel2和UpdatePanel1共同触发,
也可以增加到很多级,只要类似的改代码就可以了。
以下为源代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div style="float: left">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" Width="184px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" OnTextChanged="DropDownList1_SelectedIndexChanged" >

</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div style="float: left">
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" Width="168px" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" >


</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True" Width="160px">

</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList2" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</form>
</body>
</html>

// Default.aspx.cs文件
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.DataSource = CreateSource(0);
DropDownList1.DataTextField = "TypeName";
DropDownList1.DataValueField = "TypeID";
DropDownList1.DataBind();
int PreID = Convert.ToInt32(DropDownList1.SelectedValue);
DropDownList2.DataSource = CreateSource(PreID);
DropDownList2.DataTextField = "TypeName";
DropDownList2.DataValueField = "TypeID";
DropDownList2.DataBind();
PreID = Convert.ToInt32(DropDownList2.SelectedValue);
DropDownList3.DataSource = CreateSource(PreID);
DropDownList3.DataTextField = "TypeName";
DropDownList3.DataValueField = "TypeID";
DropDownList3.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int PreID = Convert.ToInt32(DropDownList1.SelectedValue);
if (CreateSource(PreID) != null)
{
DropDownList2.DataSource = CreateSource(PreID);
DropDownList2.DataTextField = "TypeName";
DropDownList2.DataValueField = "TypeID";
DropDownList2.DataBind();
PreID = Convert.ToInt32(DropDownList2.SelectedValue);
if (CreateSource(PreID) != null)
{
DropDownList3.DataSource = CreateSource(PreID);
DropDownList3.DataTextField = "TypeName";
DropDownList3.DataValueField = "TypeID";
DropDownList3.DataBind();
}
else
{
DropDownList3.Items.Clear();
}
}
else
{
DropDownList2.Items.Clear();
DropDownList3.Items.Clear();

}

}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
int PreID = Convert.ToInt32(DropDownList2.SelectedValue);
if (CreateSource(PreID) != null)
{
DropDownList3.DataSource = CreateSource(PreID);
DropDownList3.DataTextField = "TypeName";
DropDownList3.DataValueField = "TypeID";
DropDownList3.DataBind();
}
else
{
DropDownList3.Items.Clear();
}
}
protected ICollection CreateSource(int preId)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["NewsReleaseConnectionString"].ConnectionString;
string sqlStr="Select TypeId,TypeName From ArticleType Where PreId=' "+preId.ToString()+"'";
SqlDataAdapter sda = new SqlDataAdapter(sqlStr,conn);
DataSet ds = new DataSet();
sda.Fill(ds);
if (ds.Tables[0].Rows.Count <= 0)
{
return null;
}
else
{
return ds.Tables[0].DefaultView;
}

}
}

数据库中的相应表的SQL

USE [NewsRelease]
GO
/****** 对象: Table [dbo].[ArticleType] 脚本日期: 12/05/2007 12:49:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[ArticleType](
[TypeID] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[TypeName] [varchar](20) COLLATE Chinese_PRC_CI_AS NOT NULL,
[PreID] [bigint] NOT NULL,
[AddTime] [datetime] NULL,
CONSTRAINT [PK_ArticleType] PRIMARY KEY CLUSTERED
(
[TypeID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
[/CODE]
crazyminds110 2009-03-31
  • 打赏
  • 举报
回复
up
dk9761 2009-03-27
  • 打赏
  • 举报
回复
我这里调试这个是没有问题的~
dk9761 2009-03-26
  • 打赏
  • 举报
回复
就用UPdatePanel 就得了呗~
在每个DropDownList 外加上 UpdatePanel,
然后在 <Tragg..>中ID为触发他的控件 触发事件是 SelectedChange ,
大概思路是这样,代码记不住~ 这个机器没有vs开发环境~ 只能猜着写了
crazyminds110 2009-03-26
  • 打赏
  • 举报
回复
郁闷 还是没能解决
dk9761 2009-03-26
  • 打赏
  • 举报
回复
aspx代码
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
</div>
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">

</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList2"
EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList2_SelectedIndexChanged">
<asp:ListItem>aaa</asp:ListItem>
<asp:ListItem>bbb</asp:ListItem>
<asp:ListItem Value="ccc">ccc</asp:ListItem>

</asp:DropDownList>
</form>
</body>

cs代码
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
//DropDownList1.Visible = !DropDownList1.Visible;
this.DropDownList1.Items.Add(DropDownList2.SelectedItem.Text);
}
crazyminds110 2009-03-26
  • 打赏
  • 举报
回复
ajax: 用了里面的updatepanel triggers 属性设置了为listbox1 enventname 为selectindexchanged事件
childrenastriggers为TRUE updatemodel 为conditional
listbox1属性AUTOPOSTBACK设为TRUE,selectindexchanged事件中的代码是对listbox2的操作,代码正确无误,但是点击listbox1中的项时 listbox2中没有任何反映
请问我的设置是否有问题????为什么listbox2没有任何反应呢????
crazyminds110 2009-03-25
  • 打赏
  • 举报
回复
麻烦给点实际操作性的代码
呵呵,指导性的就不要啦
风骑士之怒 2009-03-25
  • 打赏
  • 举报
回复
利用AJAX Control ToolKit控件集里面的CascadingDropDown控件可以实现,

CascadingDropDown控件 类型:具有AJAX功能的Extender控件

功能:提供一组联动式的DropDownList,可在不Postback的状况下,通过WEB services动态从Server端(数据库中)获取DropDownList内的Item。

使用时机:在页面上呈现出分类选项,例如工厂、部门等多层次数据,item数众多,想通过多个DropDownList分类,但又不想换页(postback)时使用

111,126

社区成员

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

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

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