This is a migrated thread and some comments may be shown as answers.

Dynamic button postback

1 Answer 264 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Boris
Top achievements
Rank 1
Boris asked on 03 Nov 2016, 10:28 PM

Here is the idea: a tree view with nodes. Once a node is clicked, the server generates a panel with some buttons specific for that node. Those buttons once clicked (Click event postback) call the event handler on the server. Basically dynamically created nodes with postbacks after being clicked create buttons with postbacks. 

Here is my approach: I have a RadTreeView with some data. The RadTreeView has a "OnNodeClick" event handler that gets the actual clicked node. Then the server generates dynamically buttons on a panel based on the value in the node. Problem is that the "OnNodeClick" event handler is fired after the page load and therefore the postbacks from the buttons are not bound. So I tried another approach by adding the selected node value to the ViewState and then reading from it at Page_Load, but the problem is still the same. The chain is Page_Load -> OnNodeClicked, so the ViewState gets set only after the controls have been generated and therefore only on a second Page_Load I can actually get the value. 

Some code to make it clear:

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                    InitTreeNodesFromServices(resourceTree);
            }
            var resourceIdObj = ViewState["ResourceId"];
            if(resourceIdObj != null)
                GenerateButtonsForSelection((string)resourceIdObj);
        }
    protected void resourceTree_OnNodeClick(object sender, RadTreeNodeEventArgs e)
        {
            ViewState.Add("ResourceId", e.Node.Value);
        }

I know it's a simple issue, but I have been stuck with it for a whole day at this point. What is the right way to handle this situation? Maybe I need a different approach? Thank you. 

1 Answer, 1 is accepted

Sort by
0
Ivan Danchev
Telerik team
answered on 08 Nov 2016, 02:27 PM
Hello Boris,

We would suggest an easier approach consisting of: subscribing the TreeView to its OnClientNodeClicked client-side event handler: OnClientNodeClicked="OnClientNodeClicked", saving the clicked node's value in a hidden field on the client:
<asp:HiddenField runat="server" ID="HiddenField1"/>
<script>
    function OnClientNodeClicked(sender, args) {
        var nodeText = args.get_node().get_value();
        $telerik.$("#HiddenField1").val(nodeText);
    }
</script>

This way the node's value will be accessible in the Page_Load handler: HiddenField1.Value. After getting the value from the HiddenField you could set it back to an empty string so that it is not kept unnecessarily on every subsequent postback initiated by other controls, thus it will be set only when selecting a node and will be available on the server only in the postback initiated by the click on the TreeView's node.
 
Regards,
Ivan Danchev
Telerik by Progress
Check out the new UI for ASP.NET Core, the most complete UI suite for ASP.NET Core development on the market, with 60+ tried-and-tested widgets, based on Kendo UI.
Tags
TreeView
Asked by
Boris
Top achievements
Rank 1
Answers by
Ivan Danchev
Telerik team
Share this question
or