日期:2014-05-18  浏览次数:20423 次

根据数据库表的内容生成XML文件绑定到menu,顶者有分喽
protected void Page_Load(object sender, EventArgs e)
  {
  if (!IsPostBack)
  {
  OracleConnection conn = new OracleConnection(Page_SQL_CONN_Entity);
  conn.Open();
  OracleCommand cmd = new OracleCommand("select * from sys_action_column", conn);
  OracleDataReader dr = cmd.ExecuteReader();
  while (dr.Read())
  {
  TreeNode tn = new TreeNode();
  tn.Text = dr[1].ToString();
  tn.Value = dr[0].ToString();
  tn.NavigateUrl = "#";
  AddNodes(tn);
  TreeView1.Nodes.Add(tn);
  }
  dr.Close();
  conn.Close();
  conn.Dispose();
  }
  }

  private void AddNodes(TreeNode tn)
  {
  OracleConnection conn = new OracleConnection(Page_SQL_CONN_Entity);
  conn.Open();
  OracleCommand cmd = new OracleCommand("select id,action_name from sys_action where action_column_id=" + tn.Value, conn);
  OracleDataReader dr = cmd.ExecuteReader();
  while (dr.Read())
  {
  TreeNode subtn = new TreeNode();
  subtn.Text = dr[1].ToString();
  subtn.Value = dr[0].ToString();
  subtn.NavigateUrl = "xxx.aspx?xx=" + subtn.Value;
  tn.ChildNodes.Add(subtn);
  }
  dr.Close();
  conn.Close();
  conn.Dispose();

以上代码实现了将数据库中的两个表动态的绑定到一个TreeView上,生成了一个二级树,现在我想把它改成生成xml文件
一样的产生一个二级菜单,然后绑定到mune控件上,请大家指导一下。

------解决方案--------------------
up
------解决方案--------------------
楼主的意思是把数据写成XML文件,然后把文件作为数据源给Menu吗? 我觉得这样就涉及到IO操作了,会影响速度。
------解决方案--------------------
menu可以直接绑定xml文件么?

------解决方案--------------------
考虑使用迭代数据源的方式,参考一下这段代码,是采用迭代数据源的方式实现了一个DataSource控件:

public class CLocationDataSource : HierarchicalDataSourceControl, IHierarchicalDataSource
{

public CLocationDataSource() : base() { }

// Return a strongly typed view for the current data source control.
private CLocationDataSourceView view = null;
protected override HierarchicalDataSourceView GetHierarchicalView(string viewPath)
{
// 这里是代码的关键,viewPath可以理解为用于获取子元素集合的父元素票据,例如父节点的Value
// 对根节点的viewPath为空字符串,要注意这里的问题
viewPath = string.IsNullOrEmpty(viewPath) ? m_StartPosition.ToString() : viewPath;
view = new CLocationDataSourceView(viewPath);
return view;
}

// This can be used declaratively. To enable declarative use, 
// override the default implementation of CreateControlCollection 
// to return a ControlCollection that you can add to.
protected override ControlCollection CreateControlCollection()
{
return new ControlCollection(this);
}
}


public class CLocationDataSourceView : HierarchicalDataSourceView
{

private string _viewPath;
public CLocationDataSourceView(string viewPath)
{
// 对根节点的处理
_viewPath = string.IsNullOrEmpty(viewPath) ? "0" : viewPath;

}

public override IHierarchicalEnumerable Select()
{
// 这里用代码实现数据源的迭代体