1

我在数据库中有两个表:

CATALOGBOOK(idCA, nameCA);// kind of book
BOOK(idBOOK, nameBOOK, idCA);// info about one book

从数据库加载数据后的 TreeView:

+nameCA1
  -nameBOOK1
  -nameBOOK2
+nameCA2
  -nameBOOK1
  -nameBOOK2
.....

我的网络表单中还有一个文本框。当我选择 treenode 时,我SelectedNodeChanged Event用于在该文本框中显示书名nameBOOK[j]。SelectedNodeChanged 事件在第一个中运行良好,但是当我在nameCA[1]其中选择树节点时出现错误nameBOOK[j]nameCA[2],nameCA[3],...

我想将数据库中的数据加载到 TreeView 中:

+nameCA1
  -nameBOOK1
  -nameBOOK2
+nameCA1
  -nameBOOK1
  -nameBOOK2
.....

将数据从 DB 加载到 TreeView 的函数:

protected void Page_Load(object sender, EventArgs e)
{
    LoadTree();
}

private void LoadTree()
{
    TreeView1.Nodes.Clear();
    string sql = "select * from CATALOGBOOK";
    DataTable catalog = l.EXECUTEQUERYSQL(sql);
    for (int i = 0; i < catalog.Rows.Count; i++)
    {
        TreeNode TN = new TreeNode();
        TN.Text = catalog.Rows[i][1].ToString();//type of book
        TreeNodeSelectAction treeNodeSelectAction = TreeNodeSelectAction.None;
        TN.SelectAction = treeNodeSelectAction;
        TreeView1.Nodes.Add(TN);

        string sql1 = "select * from BOOK where idCA=" + catalog.Rows[i][0].toString();
        DataTable book = l.EXECUTEQUERYSQL(sql1);
        for (int j = 0; j < book.Rows.Count; j++)
        {
            TreeNode CN = new TreeNode(book.Rows[j][1].ToString(), book.Rows[j][0].ToString());
            CN.Text = book.Rows[j][1].ToString();//name of book
            CN.Value = book.Rows[j][0].ToString();//id of book
            TN.ChildNodes.Add(CN);
        }
    }
}//end of loadtree();

public void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
    this.TextBox1.Text = TreeView1.SelectedNode.Value.ToString();
}

我遇到的问题是:this.TextBox1.Text = TreeView1.SelectedNode.Value.ToString();

 <asp:TreeView OnSelectedNodeChanged="TreeView1_SelectedNodeChanged" ID="TreeView1"  runat="server" ImageSet="Simple" 
                    Width="183px">
                        <HoverNodeStyle BorderStyle="Inset" />
                        <LeafNodeStyle Font-Italic="True" Font-Size="Smaller" />
                        <LevelStyles>
                            <asp:TreeNodeStyle Font-Bold="True" Font-Underline="False" />
                            <asp:TreeNodeStyle Font-Underline="False" />
                            <asp:TreeNodeStyle Font-Italic="True" Font-Underline="False" />
                        </LevelStyles>
                        <SelectedNodeStyle Font-Overline="False" Font-Underline="True" />
                    </asp:TreeView> 

有错吗???

4

1 回答 1

0

在页面加载和回发时加载项目您不需要再次加载它,如果您在每次页面加载时重新加载它,您将丢失选定的值

  protected void Page_Load(object sender, EventArgs e)
   { 
       if(!Page.IsPostBack)
              LoadTree();
   }
于 2013-09-15T06:23:51.230 回答