贴出代码: 此代码已经断点测试过using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Configuration; using System.Data; using System.Data.SqlClient;public partial class newadmin_test4 : System.Web.UI.Page { private string _connstring = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { get_lv1_data(); } } protected void get_lv1_data() { SqlConnection con = new SqlConnection(_connstring); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = "select * from pro_category where pid=@pid order by shunxu"; cmd.Parameters.AddWithValue("@pid", 0); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet dst = new DataSet(); using (sda) { sda.Fill(dst); } lv1.DataSource = dst; lv1.DataBind(); } public DataSet lv2data(object lv1id) { SqlConnection con = new SqlConnection(_connstring); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = "select * from pro_category where pid=@pid order by shunxu"; cmd.Parameters.AddWithValue("@pid", lv1id); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet dst = new DataSet(); using (con) { con.Open(); sda.Fill(dst); } return dst; } public DataSet lv3data(object lv2id) { SqlConnection con = new SqlConnection(_connstring); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = "select * from pro_category where pid=@pid order by shunxu"; cmd.Parameters.AddWithValue("@pid", lv2id); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet dst = new DataSet(); using (con) { con.Open(); sda.Fill(dst); } return dst; } } 前台页面代码:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test4.aspx.cs" Inherits="newadmin_test4" %><html lang="en"> <head> <meta charset="utf-8"> <title>sf</title>
2 先生成treeview 树,然后 执行
3 调试过,值都有,可以输出3条纪录,但是 调试时候显示 count=0;
4 我觉得主要问题还是下面的函数逻辑有问题,请大哥帮看看
2 绑定数据显示了,也是正常的
3 可能我的初始化 treeview 有问题,造成的后面无法编程时绑定
4 大哥,能远程帮我看看吗?
{
string categoryid = dr["categoryid"].ToString();
foreach (TreeNode tn in TreeView1.Nodes)
{
if (dr["categoryid"].ToString() == tn.Value)
{
tn.Selected = true;
}
}
}
这个是不能这样写的,需要你自己琢磨一个递归,TreeView只能通过递归去实现
此代码已经断点测试过using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;public partial class newadmin_test4 : System.Web.UI.Page
{
private string _connstring = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
get_lv1_data();
}
} protected void get_lv1_data()
{
SqlConnection con = new SqlConnection(_connstring);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select * from pro_category where pid=@pid order by shunxu";
cmd.Parameters.AddWithValue("@pid", 0); SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet dst = new DataSet();
using (sda)
{
sda.Fill(dst);
} lv1.DataSource = dst;
lv1.DataBind();
}
public DataSet lv2data(object lv1id)
{
SqlConnection con = new SqlConnection(_connstring);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select * from pro_category where pid=@pid order by shunxu";
cmd.Parameters.AddWithValue("@pid", lv1id); SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet dst = new DataSet();
using (con)
{
con.Open();
sda.Fill(dst); }
return dst; }
public DataSet lv3data(object lv2id)
{
SqlConnection con = new SqlConnection(_connstring);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select * from pro_category where pid=@pid order by shunxu";
cmd.Parameters.AddWithValue("@pid", lv2id); SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet dst = new DataSet();
using (con)
{
con.Open();
sda.Fill(dst); }
return dst; }
}
前台页面代码:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test4.aspx.cs" Inherits="newadmin_test4" %><html lang="en">
<head>
<meta charset="utf-8">
<title>sf</title>
</head>
<body>
<form id="form1" runat="server">
<ul>
<asp:Repeater ID="lv1" runat="server">
<ItemTemplate>
<li>
<div class="div_lv1"><span class="label_lv1"><%#Eval("categoryname") %>(<%#Eval("shunxu") %>)</span><br class="clear" /></div>
<ul class="ul_lv2">
<asp:Repeater ID="lv2" runat="server" DataSource='<%# lv2data(Eval("id")) %>'>
<ItemTemplate>
<li>
<div class="div_lv2"><span class="label_lv2"><%#Eval("categoryname") %>(<%#Eval("shunxu") %>)</span><br class="clear" />
<ul class="ul_lv3">
<asp:Repeater ID="lv3" runat="server" DataSource='<%# lv3data(Eval("id")) %>' >
<ItemTemplate>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" DataTextField='<%#Eval("categoryname") %>' DataValueField='<%#Eval("id") %>'> </asp:CheckBoxList>
</ItemTemplate>
</asp:Repeater> </ul>
</div><br class="clear" />
</li> </ItemTemplate> </asp:Repeater> </ul> </li> <br class="clear" />
</ItemTemplate>
</asp:Repeater>
</ul>
</form>
</body>
</html>但是得不到想要的结果
想要的是这种结果:
//通过下拉列表绑定数据显示在TreeView控件节点
SqlConnection conn; //创建SqlConnection对象
SqlCommand cmd; //创建SqlCommamd对象
SqlDataAdapter sda; //创建SqlDataAdapter对象
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)//如果页面第一次加载
{
string str = System.Configuration.ConfigurationManager.AppSettings["conn"].ToString();
tvcourse.ShowCheckBoxes = TreeNodeTypes.Leaf;//节点添加复选框
conn = new SqlConnection(str); //建立连接
cmd = new SqlCommand("select distinct(courseType) from tbCourse", conn);
sda = new SqlDataAdapter(cmd); //实例化SqlDataAdapter对象
DataSet ds = new DataSet();
sda.Fill(ds);
ddlType.DataSource = ds;
ddlType.DataTextField = "courseType";
ddlType.DataBind();
TreeNode TN = new TreeNode(ddlType.SelectedItem.Text);
cmd = new SqlCommand("select * from tbCourse where courseType='"+ddlType.SelectedItem.Text+"'", conn);
sda = new SqlDataAdapter(cmd);
ds = new DataSet();
sda.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TN.ChildNodes.Add(new TreeNode(ds.Tables[0].Rows[i]["courseName"].ToString()));
}
tvcourse.Nodes.Add(TN);
}
}
//更改分类重新绑定数据显示
protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
{
tvcourse.Nodes.Clear();
string str = System.Configuration.ConfigurationManager.AppSettings["conn"].ToString();
conn = new SqlConnection(str);
TreeNode TN = new TreeNode(ddlType.SelectedItem.Text);
cmd = new SqlCommand("select * from tbCourse where courseType='" + ddlType.SelectedItem.Text + "'", conn);
sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TN.ChildNodes.Add(new TreeNode(ds.Tables[0].Rows[i]["courseName"].ToString()));
}//CodeGo.net/
tvcourse.Nodes.Add(TN);
tvcourse.ExpandAll();
}
//选中复选框点击按钮把所选科目添加到数据表
protected void Button1_Click(object sender, EventArgs e)
{
lblinfo.Text = "";
if (tvcourse.CheckedNodes.Count > 0)
{
foreach (TreeNode childNode in tvcourse.CheckedNodes)
{
lblinfo.Text += childNode.Text + " ";
}
}
}