asp.net(#) 新手求教点击treeview控件的子节点在gridwiew中显示

cuifengke 2011-05-12 01:34:05
有一个Access数据库里面有14张表,怎样用treeview控件显示表名,点击表名在右边的gridwiew中显示这张表?
新手求解
...全文
69 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
cuifengke 2011-05-13
  • 打赏
  • 举报
回复
谢谢各位大哥~
暖枫无敌 2011-05-12
  • 打赏
  • 举报
回复

分三个页面,没有Access实例数据库,没测试,基本大概这个写法
Default.aspx页面s
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>目录树</title>
<style type="text/css">
html
{
overflow-x: hidden;
}
</style>
</head>
<frameset cols="1,*,1" frameborder="no" border="0" framespacing="0">
<frame src="about:blank"></frame>
<frameset cols="178,*" frameborder="no" border="0" framespacing="0" id="MainFrame">
<frame src="Left.aspx" name="left" scrolling="auto" id="left" title="left" />
<frame src="Center.aspx" name="center" id="center" title="center" scrolling="auto" />
</frameset>
<frame src="about:blank"></frame>
</frameset>
<noframes>
</noframes>
</html>

Left.aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Left.aspx.cs" Inherits="Left" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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">
</asp:TreeView>

</div>
</form>
</body>
</html>
后台cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;

public partial class Left : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindAccessTree();
}
}

public void BindAccessTree()
{
//Access 2007数据库
string strConnection = "Provider = Microsoft.ACE.OLEDB.12.0;"; //C#读取Excel的连接字符串
strConnection += @"Data Source = E:\Access\AccessDB.accdb "; //指定数据库在硬盘的物理位置
using (OleDbConnection objConnection = new OleDbConnection(strConnection)) //用using替代objConnection.Close()
{
objConnection.Open(); //打开连接
DataTable tblSch = objConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
DataView view = tblSch.DefaultView;
view.RowFilter = "table_type='table' ";
TreeNode node;
foreach (DataRowView drv in view)
{
node = new TreeNode();
node.Text = drv["Table_Name"].ToString();
node.NavigateUrl = "Center.aspx?table_name='" + drv["Table_Name"] + "'";
this.TreeView1.Nodes.Add(node);
}
}
}
}

Center.aspx页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Center.aspx.cs" Inherits="Center" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>

</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;

public partial class Center : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string tableName = Request.QueryString["Table_Name"].ToString();
BindGridView(tableName);
}
}

public void BindGridView(string tableName)
{
string strConnection = "Provider = Microsoft.ACE.OLEDB.12.0;"; //C#读取Excel的连接字符串
strConnection += @"Data Source = E:\Access\CSharpTest.accdb "; //指定数据库在硬盘的物理位置
using (OleDbConnection objConnection = new OleDbConnection(strConnection)) //用using替代objConnection.Close()
{
objConnection.Open(); //打开连接
string strSQL = "select * from "+tableName;
OleDbDataAdapter Comm = new OleDbDataAdapter(strSQL, objConnection);

DataSet ds = new DataSet(); //DataSet 初试化
Comm.Fill(ds, "temp");
this.GridView1.DataSource = ds.Tables["temp"].DefaultView;
this.GridView1.DataBind();
}
}
}

子夜__ 2011-05-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 cuifengke 的回复:]

谢谢你!能做个示范吗?在线等
[/Quote]
先把你的表明读出来

点击节点触发

select * from @table...

@table参数 把你的节点的文本传进去。。。

右边绑定。
cuifengke 2011-05-12
  • 打赏
  • 举报
回复
谢谢你!能做个示范吗?在线等
bu在服务区 2011-05-12
  • 打赏
  • 举报
回复
你把数据库里面所有的表名查询出来,
然后绑定到treeview,
当点击当前节点是时候获取当前节点的表名,
然后跟据这个表名查询该表中的数据然后绑定到gridwiew上显示~!~~

62,244

社区成员

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

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

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

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