2

I am allowing a user to check the checkboxes associated with nodes in my treeview.

I would like to prompt the user for their input as soon as a specific node is checked. For Example:

if node#2 is checked then get user input
  1. how do i do this? is the best way c# with javascript? if so , then how?

  2. how would i get a list of selected notes after submit button is clicked?

1
  • @Asdfg i have flagged you so that the moderator removes your picture Commented Jun 1, 2012 at 20:50

3 Answers 3

2
+50
//get a list of selected nodes after submit button is clicked
 protected void submitBtn_Click(object sender, EventArgs e)
 {
           if (this.tree.CheckedNodes.Count > 0)
            {
                // Iterate through the CheckedNodes collection and display the selected nodes.
                foreach (TreeNode node in tree.CheckedNodes)
                {
                   Response.Write(node.Value + "<br />"); 
                }
            }

            else
            {
                Response.Write("No node selected.");

            }
}

To get the selected node when user check a node you have to fire postback in javascript in head tag Like

<script language="javascript" type="text/javascript">
        function postBackByObject() {
            var o = window.event.srcElement;
            if (o.tagName == "INPUT" && o.type == "checkbox") {
                __doPostBack("", "");
            }
        }

</script>

and put above code inside TreeNodeCheckChanged event of Treeview like

protected void tree_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
{

        if (tree.CheckedNodes.Count > 0)
        {
            // Iterate through the CheckedNodes collection and display the selected nodes.
            foreach (TreeNode node in tree.CheckedNodes)
            {
                Response.Write (node.Value + "<br />");

            }
        }

        else
        {
            Response.Write("No items selected.");

        }

    }
Sign up to request clarification or add additional context in comments.

Comments

2

The simplest way is to just handle an OnChange event of each CheckBox with a JavaScript function, which in turn is displaying a prompt box on select - whose input then gets saved in an associated Hidden field (or data-* attribute in HTML5), which gets cleared on unselect.

Then on submit in C# you iterate through all the CheckBox controls for a checked attribute, and all Hidden fields (or data-* attribute in HTML5) for their input.

Comments

1

You can use

treeview.Attributes.Add("onclick","prompt(this);");

on Page_Load this will bind a javascript funciton prompt(sender); to your treeview, & in prompt() function you can find which element raised the event by using this & sender or you can use event/e to get information about event raising element, for more info on event go here

if you need your user to provide a good responsive experiece then javascript is best way to do it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.