日期:2014-05-18 浏览次数:20590 次
<%@ Page Language="C#" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.Sql" %> <%@ Import Namespace="System.Data.SqlClient" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { using (SqlConnection conn = new SqlConnection(connectionString)) { using (SqlDataAdapter da = new SqlDataAdapter(SQLString, conn)) { using (DataTable dt = new DataTable()) { conn.Open(); da.Fill(dt); conn.Close(); foreach (DataRow dr in dt.Rows) { this.TreeView1.Nodes.Add(new TreeNode(dr["stu_name"].ToString(), dr["stu_id"].ToString())); } } } } } protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e) { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlCommand comm = new SqlCommand("SELECT * FROM stu_table WHERE stu_id='" + this.TreeView1.SelectedNode.Value + "'", conn)) { using (SqlDataReader dr = comm.ExecuteReader()) { // show data } } } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> <asp:TreeView ID="TreeView1" runat="server" ShowLines="true" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged"> </asp:TreeView> <asp:Label ID="Label1" runat="server"></asp:Label> </div> </form> </body> </html>
------解决方案--------------------
<%@ Page Language="C#" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.Sql" %> <%@ Import Namespace="System.Data.SqlClient" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { using (SqlConnection conn = new SqlConnection(connectionString)) { using (SqlDataAdapter da = new SqlDataAdapter(SQLString, conn)) { using (DataTable dt = new DataTable()) { conn.Open(); da.Fill(dt); conn.Close(); foreach (DataRow dr in dt.Rows) { this.TreeView1.Nodes.Add(new TreeNode(dr["stu_name"].ToString(), dr["stu_id"].ToString())); } } } } } protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e) { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlCommand comm = new SqlCommand("SEL