Webform中SelectedNodeChanged事件的错误

时间:2013-09-15 06:12:38

标签: c# asp.net webforms treeview

我在DB中有两个表:

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

从DB加载数据后的TreeView:

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

我的网络表单中也有一个文本框。当我选择treenode SelectedNodeChanged Event时,我使用nameBOOK[j]在该文本框中显示图书的名称。 SelectedNodeChanged事件在第一个nameCA[1]下工作正常,但在nameBOOK[j]

中选择treenode nameCA[2],nameCA[3],...时出错

我想将数据从DB加载到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> 

有没有错误???

1 个答案:

答案 0 :(得分:0)

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

  protected void Page_Load(object sender, EventArgs e)
   { 
       if(!Page.IsPostBack)
              LoadTree();
   }