Click here to Skip to main content
15,912,207 members
Home / Discussions / ASP.NET
   

ASP.NET

 
GeneralRe: Active Directory User Creation Pin
legionnaire2-Oct-06 6:24
legionnaire2-Oct-06 6:24 
QuestionFile Download Response..... Pin
Neo7869229-Sep-06 9:35
Neo7869229-Sep-06 9:35 
AnswerRe: File Download Response..... Pin
Jim Conigliaro29-Sep-06 10:45
Jim Conigliaro29-Sep-06 10:45 
QuestionRe: File Download Response..... Pin
Neo7869230-Sep-06 4:51
Neo7869230-Sep-06 4:51 
QuestionInherited TreeView Nodes and typeof Pin
perlmunger29-Sep-06 7:48
perlmunger29-Sep-06 7:48 
AnswerRe: Inherited TreeView Nodes and typeof Pin
minhpc_bk29-Sep-06 15:03
minhpc_bk29-Sep-06 15:03 
GeneralRe: Inherited TreeView Nodes and typeof Pin
perlmunger30-Sep-06 9:29
perlmunger30-Sep-06 9:29 
GeneralRe: Inherited TreeView Nodes and typeof Pin
minhpc_bk1-Oct-06 2:43
minhpc_bk1-Oct-06 2:43 
Here you want to use multi different types of the tree node such as Child1TreeNode, Child2TreeNode, so you need a way to determine which object should be created when the treenode is rebuilt on postback. And the ViewState comes to mind as an option when you can use it to persist the node type in order to recreate the tree node. However, when you create a tree node in the overriden CreateNode method in the custom treeview, the ViewState of the tree node has not been loaded yet, so you will do that as soon as the ViewState is loaded. In the CreateNode, you simply return the base class of your two node types, say ChildTreeNode, and you will replace this base node with the exact type after the node type is determined. Below is the sample code to demonstrate this thing:

public class ExTreeView : TreeView
{
    protected override TreeNode CreateNode()
    {
        return new ChildTreeNode(this);
    }
}

public class ChildTreeNode : TreeNode
{
    public ChildTreeNode() { }

    public ChildTreeNode(string nodeType) { m_NodeType = nodeType; }

    public ChildTreeNode(ExTreeView owner) { this.m_Owner = owner; }

    private ExTreeView m_Owner;
    internal ExTreeView Owner { get { return m_Owner; } }

    private string m_NodeType;
    internal string NodeType
    {
        get { return m_NodeType; }
        set { m_NodeType = value; }
    }

    protected override object SaveViewState()
    {
        object baseState = base.SaveViewState();
        return new Pair(baseState, NodeType);
    }

    protected override void LoadViewState(object savedState)
    {
        Pair p = savedState as Pair;
        base.LoadViewState(p.First);
        NodeType = p.Second as string;

    //The node type is hard coded for the sake of simplicity.
        TreeNode newNode;
        if (NodeType == "Child1TreeNode") { newNode = new Child1TreeNode(); }
        else if (NodeType == "Child2TreeNode") { newNode = new Child2TreeNode(); }
        else { newNode = new TreeNode(); }

        newNode.Text = this.Text;
        newNode.Value = this.Value;

        ReplaceCurrentNode(this, newNode);
    }

    private void ReplaceCurrentNode(ChildTreeNode currentNode, TreeNode newNode)
    {
        TreeNodeCollection nodes;
        if (currentNode.Parent == null)
            nodes = currentNode.Owner.Nodes;
        else
            nodes = currentNode.ChildNodes;

        int index = nodes.IndexOf(currentNode);
        nodes.Remove(currentNode);
        nodes.AddAt(index, newNode);
    }
}

public class Child1TreeNode : ChildTreeNode
{
    public Child1TreeNode() : base("Child1TreeNode") { }
}

public class Child2TreeNode : ChildTreeNode
{
    public Child2TreeNode() : base("Child2TreeNode") { }
}


To make the sample code simpler, you may think of only using a single custom type instead of multi types to represent the tree nodes, and you can have an additional property to represent the node type. Doing so, you only need to recreate this node type in the overriden CreateNode method of the custom treeview.

Another option is that you don't need to create a custom treeview control and override the CreateNode method, what you need to do is to disable the ViewState by setting EnableViewState to false, and in the Page_Load you don't check the IsPostBack and leave the code to create the tree nodes outside the if statement.
QuestionTableAdapter info Pin
robert11029-Sep-06 7:40
robert11029-Sep-06 7:40 
AnswerRe: TableAdapter info Pin
Not Active29-Sep-06 7:58
mentorNot Active29-Sep-06 7:58 
AnswerRe: TableAdapter info Pin
sanju027629-Sep-06 9:34
sanju027629-Sep-06 9:34 
Questionphp Pin
amaneet29-Sep-06 6:39
amaneet29-Sep-06 6:39 
AnswerRe: php Pin
nguyenvhn29-Sep-06 16:24
nguyenvhn29-Sep-06 16:24 
Questionphp Pin
amaneet29-Sep-06 6:25
amaneet29-Sep-06 6:25 
AnswerRe: php Pin
Not Active29-Sep-06 7:35
mentorNot Active29-Sep-06 7:35 
QuestionWeb Control Designing Questions Pin
Xiaoming Qian29-Sep-06 4:41
Xiaoming Qian29-Sep-06 4:41 
QuestionKeeping focus on control after postback?? Pin
Goalie3529-Sep-06 3:54
Goalie3529-Sep-06 3:54 
AnswerRe: Keeping focus on control after postback?? Pin
postmaster@programmingknowledge.com29-Sep-06 4:48
postmaster@programmingknowledge.com29-Sep-06 4:48 
AnswerRe: Keeping focus on control after postback?? Pin
Not Active29-Sep-06 5:38
mentorNot Active29-Sep-06 5:38 
AnswerRe: Keeping focus on control after postback?? Pin
amaneet29-Sep-06 6:29
amaneet29-Sep-06 6:29 
GeneralRe: Keeping focus on control after postback?? Pin
Not Active29-Sep-06 7:32
mentorNot Active29-Sep-06 7:32 
QuestionGridView RowCommand Pin
John Gathogo29-Sep-06 3:24
John Gathogo29-Sep-06 3:24 
AnswerRe: GridView RowCommand Pin
Sathesh Sakthivel29-Sep-06 3:49
Sathesh Sakthivel29-Sep-06 3:49 
AnswerRe: GridView RowCommand Pin
Kschuler29-Sep-06 11:08
Kschuler29-Sep-06 11:08 
Questionmailto: Issue Pin
Brendan Vogt29-Sep-06 2:34
Brendan Vogt29-Sep-06 2:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.