Question

I use a RadTree with custom template of its elements. Each element has also 3 radio buttons as RadioButtonList, defined in a custom template. The code used to create items of new radTree is:

 RadTreeNode node = new RadTreeNode(usedName, value); // just some name and value
 node.NodeTemplate = new AdminFunctionsPhenoTreeTemplate(); // makes buttons
 node.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;
 Tree.Nodes.Add(node); // adding node to RadTree.
// selections.
 RadioButtonList rbRights = (RadioButtonList)node.FindControl("rbRights");
if (rbRights != null)
 {
 rbRights.SelectedIndex= accessLevel; // set correct radio button using rbRights.SelectedIndex
 }
 node.DataBind();

This code successfully creates desired nodes and assignment rbRights.SelectedIndex= accessLevel indeed sets desired radio button as instructed. It works for root tree and for subtrees that I create on callback call on demand.

The very same code used to create sub-trees in server side callback - I use dynamic creation of subtrees after user wants to expand the tree. The only formal difference, it attaches new subtree to Nodes property of a parent node (as opposed to RadTree "Tree" object in example). This is all standard.

When I receive a postback, I read the values of radio buttons like this:

List<RadTreeNode> alls = (List<RadTreeNode>)Tree.GetAllNodes();
   foreach (RadTreeNode node in alls)
        {
            RadioButtonList rbRights = (RadioButtonList)node.FindControl("rbRights");
            accessLevel = rbRights.SelectedIndex; // here I read my radios
         }

Contents of tree read fine, and sub-trees buttons are read fine (those created via callback call). But buttons of primary root tree, created by the snippet above, always return default value, neither the one I inserted either in creation code (albeit it was reflected on screen) nor that I selected by mouse.

Template is coded like this:

class AdminFunctionsPhenoTreeTemplate : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            container.Controls.Add(new LiteralControl("<table style=\"margin:0px;padding:0px;\"><tr><td>"));
            Label label1 = new Label();
            label1.ID = "ItemLabel";
            label1.Text = "Text";
            label1.Font.Size = 8;
            // label1.Font.Bold = true;
            label1.DataBinding += new EventHandler(label1_DataBinding);

            container.Controls.Add(label1);
            container.Controls.Add(new LiteralControl("</td><td>"));
            RadioButtonList rbRights = new RadioButtonList();
            rbRights.ID = "rbRights";
            rbRights.Items.Add(new ListItem("r/o"));
            rbRights.Items.Add(new ListItem("r/w"));
            rbRights.Items.Add(new ListItem("r/w/meta"));
            rbRights.SelectedIndex = 0;
            rbRights.RepeatDirection = RepeatDirection.Horizontal;
            container.Controls.Add(rbRights);
            container.Controls.Add(new LiteralControl("</td></tr></table>"));
        }

        private void label1_DataBinding(object sender, EventArgs e)
        {
            Label target = (Label)sender;
            RadTreeNode node = (RadTreeNode)target.BindingContainer;
            string nodeText = (string)DataBinder.Eval(node, "Text");
            target.Text = nodeText;
        }

    }

I wonder what I am missing, please help me to make this work!

Was it helpful?

Solution

Solved:

on postback, there was a stray Tree.DataBind() call happening in onLoad.

This was resetting values coming from client to default template values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top