Dynamic button postback

2 posts, 0 answers
  1. Boris
    Boris avatar
    1 posts
    Member since:
    Nov 2016

    Posted 03 Nov 2016 Link to this post

    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)
                var resourceIdObj = ViewState["ResourceId"];
                if(resourceIdObj != null)
        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. 

  2. Ivan Danchev
    Ivan Danchev avatar
    1617 posts

    Posted 08 Nov 2016 Link to this post

    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"/>
        function OnClientNodeClicked(sender, args) {
            var nodeText = args.get_node().get_value();

    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.
    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.
Back to Top