Tuesday, March 13, 2012

Set selected node in Treeview control in ASP:NET 2.0 ? Anyone know this ?

Hello,

Iam using Treeview control in asp.net 2.0. But have a problem.

I use NavigateUrl BUT then viewstate is lost when clicked on a link i
the menu. And the selected node is lost.

Then i have to set the selected node manually, how can i do it ? i have
tried but without luck :(
I have posted my menu code here.

namespace DayDream.Admin.UC
{
public partial class AdminMenu : DayDreamBaseUserControl
{
public string SelectedURL;

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

}

protected void PopulateNode(Object sender, TreeNodeEventArgs e)
{

// Call the appropriate method to populate a node at a
particular level.
switch (e.Node.Depth)
{
case 0:
// Populate the first-level nodes.
PopulateTopLevelNodes(e.Node);
break;
case 1:
// Populate the second-level nodes.
PopulateChildern(e.Node);
break;
default:
// Do nothing.
break;
}

}

private void PopulateTopLevelNodes(TreeNode node)
{
DayDreamBasePage basePage = new DayDreamBasePage();

for (int x = 0; x <
basePage.GetAllPages().Tables[0].Rows.Count; x++)
{
if
(basePage.GetAllPages().Tables[0].Rows[x]["ParentPageID"].ToString().Length
<= 0)
{

TreeNode TopLevel = new TreeNode();

//TopLevel.NavigateUrl =
basePage.GetAllPages().Tables[0].Rows[x]["Template"].ToString() +
"?id=" + basePage.GetAllPages().Tables[0].Rows[x]["PageID"].ToString();
TopLevel.NavigateUrl =
"../../"+basePage.GetAllPages().Tables[0].Rows[x]["Template"].ToString()
+ "?id=" +
basePage.GetAllPages().Tables[0].Rows[x]["PageID"].ToString();
TopLevel.Text =
basePage.GetAllPages().Tables[0].Rows[x]["PageName"].ToString();
TopLevel.Value =
basePage.GetAllPages().Tables[0].Rows[x]["PageID"].ToString();

// Add root node to TreeView
TopLevel.PopulateOnDemand = true;
TopLevel.SelectAction =
TreeNodeSelectAction.SelectExpand;
node.ChildNodes.Add(TopLevel);

}

}

}

private void PopulateChildern(TreeNode node)
{

DayDreamBasePage basePage = new DayDreamBasePage();

DataSet Childrens = new DataSet();
Childrens =
basePage.GetChildrenPages(Int32.Parse(node.Value)) ;

for (int y = 0; y < Childrens.Tables[0].Rows.Count; y++)
{

TreeNode newNode = new TreeNode();
newNode.NavigateUrl = "../../" +
Childrens.Tables[0].Rows[y]["Template"].ToString() + "?id=" +
Childrens.Tables[0].Rows[y]["PageID"].ToString();
newNode.Text =
Childrens.Tables[0].Rows[y]["PageName"].ToString();
newNode.Value =
Childrens.Tables[0].Rows[y]["PageID"].ToString();

// Set additional properties for the node.
newNode.SelectAction =
TreeNodeSelectAction.SelectExpand;

// Add the new node to the ChildNodes collection of the
parent node.
node.ChildNodes.Add(newNode);

DataSet ChildrensSub = new DataSet();
int ChildrensSubCount =
basePage.GetChildrenPages(Int32.Parse(newNode.Valu e)).Tables[0].Rows.Count;

if (ChildrensSubCount > 0)
{
PopulateChildern(newNode);
}

}
}

}
}

Thanks
JeppeOn Page_Load event you are not checking if Page isPostBack

If Not (Page.IsPostBack) {
DataBind();
}

Sandeep

"jesper_lofgren@.yahoo.se" wrote:

> Hello,
> Iam using Treeview control in asp.net 2.0. But have a problem.
> I use NavigateUrl BUT then viewstate is lost when clicked on a link i
> the menu. And the selected node is lost.
> Then i have to set the selected node manually, how can i do it ? i have
> tried but without luck :(
> I have posted my menu code here.
>
> namespace DayDream.Admin.UC
> {
> public partial class AdminMenu : DayDreamBaseUserControl
> {
> public string SelectedURL;
> protected void Page_Load(object sender, EventArgs e)
> {
> DataBind();
>
> }
> protected void PopulateNode(Object sender, TreeNodeEventArgs e)
> {
> // Call the appropriate method to populate a node at a
> particular level.
> switch (e.Node.Depth)
> {
> case 0:
> // Populate the first-level nodes.
> PopulateTopLevelNodes(e.Node);
> break;
> case 1:
> // Populate the second-level nodes.
> PopulateChildern(e.Node);
> break;
> default:
> // Do nothing.
> break;
> }
> }
>
> private void PopulateTopLevelNodes(TreeNode node)
> {
> DayDreamBasePage basePage = new DayDreamBasePage();
> for (int x = 0; x <
> basePage.GetAllPages().Tables[0].Rows.Count; x++)
> {
> if
> (basePage.GetAllPages().Tables[0].Rows[x]["ParentPageID"].ToString().Length
> <= 0)
> {
> TreeNode TopLevel = new TreeNode();
> //TopLevel.NavigateUrl =
> basePage.GetAllPages().Tables[0].Rows[x]["Template"].ToString() +
> "?id=" + basePage.GetAllPages().Tables[0].Rows[x]["PageID"].ToString();
> TopLevel.NavigateUrl =
> "../../"+basePage.GetAllPages().Tables[0].Rows[x]["Template"].ToString()
> + "?id=" +
> basePage.GetAllPages().Tables[0].Rows[x]["PageID"].ToString();
> TopLevel.Text =
> basePage.GetAllPages().Tables[0].Rows[x]["PageName"].ToString();
> TopLevel.Value =
> basePage.GetAllPages().Tables[0].Rows[x]["PageID"].ToString();
> // Add root node to TreeView
> TopLevel.PopulateOnDemand = true;
> TopLevel.SelectAction =
> TreeNodeSelectAction.SelectExpand;
> node.ChildNodes.Add(TopLevel);
>
> }
> }
> }
>
> private void PopulateChildern(TreeNode node)
> {
> DayDreamBasePage basePage = new DayDreamBasePage();
> DataSet Childrens = new DataSet();
> Childrens =
> basePage.GetChildrenPages(Int32.Parse(node.Value)) ;
> for (int y = 0; y < Childrens.Tables[0].Rows.Count; y++)
> {
> TreeNode newNode = new TreeNode();
> newNode.NavigateUrl = "../../" +
> Childrens.Tables[0].Rows[y]["Template"].ToString() + "?id=" +
> Childrens.Tables[0].Rows[y]["PageID"].ToString();
> newNode.Text =
> Childrens.Tables[0].Rows[y]["PageName"].ToString();
> newNode.Value =
> Childrens.Tables[0].Rows[y]["PageID"].ToString();
> // Set additional properties for the node.
> newNode.SelectAction =
> TreeNodeSelectAction.SelectExpand;
> // Add the new node to the ChildNodes collection of the
> parent node.
> node.ChildNodes.Add(newNode);
> DataSet ChildrensSub = new DataSet();
> int ChildrensSubCount =
> basePage.GetChildrenPages(Int32.Parse(newNode.Valu e)).Tables[0].Rows.Count;
> if (ChildrensSubCount > 0)
> {
> PopulateChildern(newNode);
> }
> }
> }
>
> }
> }
>
> Thanks
> Jeppe
>

0 comments:

Post a Comment