高分相送,dropdownlist级联添加问题?
现在要做一个dropdownlist级联..
效果如下:
|-(这只是一个样式,无所谓的)作为一级分类
|-中国
|-北京市
|-海淀区
|-韩国
当我选择一级分类时,我进行添加操作,就会加到最顶级里去
效果如下:
|-中国
|-北京市
|-海淀区
|-韩国
|-美国
当我选择中国时,我进行添加操作,就会加到中国里去
效果如下:
|-中国
|-北京市
|-海淀区
|-湖南省
当我选择北京市进行添加的时候,效果如下
|-中国
|-北京市
|-海淀区
|-XX区
|-湖南省
数据库表结构如下:
[id] [int] IDENTITY(1,1) NOT NULL,--编号,自动增长
[lan] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,--多国化语言(这里可以不用到)
[name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,--地区名称
[parentid] [int] NOT NULL,--父级编号
[num] [nvarchar](50),--编号
请问哪位大虾可以帮小弟实现一下?我们主管说是用到递归,但是小弟不会..
请各位大虾帮帮忙..
还有,我不需要用到JS.我用的是vs2003 .net1.1版本的.
[解决办法]
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class ascx_yhzc : System.Web.UI.UserControl
{
Maticsoft.BLL.users mbu = new Maticsoft.BLL.users();
Maticsoft.Model.users mbus = new Maticsoft.Model.users();
Maticsoft.BLL.sheng mbsheng = new Maticsoft.BLL.sheng();
Maticsoft.BLL.shi mbshi = new Maticsoft.BLL.shi();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
binddro1();
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
if (TextBox1.Text != null)
{
DataSet ds = new DataSet();
string name = TextBox1.Text.Trim();
string strWhere = " and u.usname='"+name+"'";
ds = mbu.GetList(strWhere);
string name1 = TextBox1.Text.Trim().ToString();
string pass = TextBox2.Text.Trim().ToString();
string sex = "";
if (RadioButtonList1.SelectedItem.Equals("男"))
{
sex = "男";
}
else
{
sex = "女";
}
string age = TextBox6.Text.Trim().ToString();
string tel = TextBox7.Text.Trim().ToString();
string qq = TextBox8.Text.Trim().ToString();
string email = TextBox9.Text.Trim().ToString();
string sheng = DropDownList1.SelectedValue.ToString();
string shi = DropDownList2.SelectedValue.ToString();
if (TextBox1.Text.Equals("")||ds.Tables[0].Rows.Count>0)
{
Response.Write("<script>alert('已经存在的用户名');</script>");
TextBox1.Text = "";
}
else if (TextBox2.Text.Equals("") || TextBox2.Text.Length > 16 || TextBox2.Text.Length < 6)
{
Response.Write("<script>alert('请输入6-16位的密码');</script>");
TextBox2.Text = "";
}
else if (TextBox3.Text != TextBox2.Text)
{
Response.Write("<script>alert('两次密码输入不一致');</script>");
TextBox2.Text = "";
TextBox3.Text = "";
}
else if (TextBox6.Text.Equals(""))
{
Response.Write("<script>alert('请输入年龄');</script>");
TextBox6.Text="";
}
else if (TextBox7.Equals(""))
{
Response.Write("<script>alert('请填写电话');</script>");
}
else if (TextBox8.Equals(""))
{
Response.Write("<script>alert('请填写QQ号');</script>");
}
else if (sheng.Equals(""))
{
Response.Write("<script>alert('请选择省份');</script>");
}
else if (shi.Equals(""))
{
Response.Write("<script>alert('请选择市');</script>");
}
else
{
mbus.usage = age;
mbus.usemail = email;
mbus.usname = name1;
mbus.usqq = qq;
mbus.ussex = sex;
mbus.ustel = tel;
mbus.uspass = pass;
mbus.shengid = Convert.ToInt32(DropDownList1.SelectedValue.ToString());
mbus.shiid = Convert.ToInt32(DropDownList2.SelectedValue.ToString());
int i = mbu.Add(mbus);
if (i > 0)
{
Session["name"] = name;
Response.Write("<script>alert('注册成功');location.href='huiyuan/huiyuanzhongxin.aspx';</script>");
}
else
{
Response.Write("<script>alert('出错啦!注册失败啦!')</script>");
}
}
}
}
public void binddro1()
{
DropDownList1.Items.Insert(0, new ListItem("<-选择->", "0"));
}
protected void Button2_Click1(object sender, EventArgs e)
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox6.Text = "";
TextBox7.Text = "";
TextBox8.Text = "";
TextBox9.Text = "";
}
protected void Button3_Click(object sender, EventArgs e)
{
Response.Redirect("mimazhaohui.aspx");
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet ds = new DataSet();
int j = Convert.ToInt32(DropDownList1.SelectedValue.ToString());
string strWhere = " and s.shengid='" + j + "'";
ds = mbshi.GetList(strWhere);
DropDownList2.DataSource = ds;
DropDownList2.DataTextField = "shiname";
DropDownList2.DataValueField = "shiid";
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, new ListItem("<-选择->", "0"));
}
protected void TextBox1_Disposed(object sender, EventArgs e)
{
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
DataSet ds = new DataSet();
string name = TextBox1.Text.Trim();
string strWhere = " and u.usname='" + name + "'";
ds = mbu.GetList(strWhere);
if (ds.Tables[0].Rows.Count > 0)
{
Label1.Visible = false;
Label2.Visible = true;
}
else
{
Label1.Visible = true;
Label2.Visible = false;
}
}
}
[解决办法]
楼主,你这个容易啊,当你选择一项时,点击添加后,在后台得到dropdownlist 的当前选中值(记录的ID),然后在datatable里添加一记录,他的父ID设置成当前选择的ID就是咯。然后再重新将datatble绑定到dropdownlist
[解决办法]
namespace XR.Web { using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Data.Sql; using System.Data.SqlClient; public partial class Default2 : System.Web.UI.Page { DataTable Tables = new DataTable(); DataColumn DC = new DataColumn(); DataRow DR; ProductCategoryBLL Category = new ProductCategoryBLL(); protected void Page_Load(object sender, EventArgs e) { DataTable DT = Category.GetDataTable("YLProductCategory", 0); CreateDataTable(); GetTree(DT, 0, 0); DropDownList1.DataSource = Tables; DropDownList1.DataTextField = "Title"; DropDownList1.DataValueField = "ID"; DropDownList1.DataBind(); } public void GetTree(DataTable DT,int PID,int Depth) { DataView DV = DT.DefaultView; DV.RowFilter = "ParentID=" + PID; string str = string.Empty; if (Depth == 0) { str = ""; } else if (Depth == 1) { str = "├"; } else { for (int i = 0; i < Depth; i++) { str =str+"├" + "" + "-"; } } foreach (DataRowView Drv in DV) { int ID = int.Parse(Drv["ID"].ToString()); string Title = Drv["Title"].ToString(); int ParentID = int.Parse(Drv["ParentID"].ToString()); int Dep = int.Parse(Drv["Depth"].ToString()); DR = Tables.NewRow(); DR["ID"] = ID; DR["Title"] = str + Title; DR["ParentID"] = ParentID; DR["Depth"] = Dep; Tables.Rows.Add(DR); int n = Dep; n++; GetTree(DT,ID, n); } } public void CreateDataTable() { DC = new DataColumn(); DC.ColumnName = "ID"; DC.DataType = System.Type.GetType("System.Int32"); Tables.Columns.Add(DC); DC = new DataColumn(); DC.ColumnName = "Title"; DC.DataType = System.Type.GetType("System.String"); Tables.Columns.Add(DC); DC = new DataColumn(); DC.ColumnName = "ParentID"; DC.DataType = System.Type.GetType("System.Int32"); Tables.Columns.Add(DC); DC = new DataColumn(); DC.ColumnName = "Depth"; DC.DataType = System.Type.GetType("System.Int32"); Tables.Columns.Add(DC); } } } 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ws_hgo/archive/2009/11/20/4842026.aspx
[解决办法]
当你选中个item时 根据他的value(不同级别的itemvalue当然不同) 操作数据库