Command event on programmatically added RadDock controls

2 posts, 1 answers
  1. Hrvach
    Hrvach avatar
    19 posts
    Member since:
    Aug 2012

    Posted 12 Mar 2012 Link to this post


    I have a question regarding programmatically created RadDock controls. I can't figure out how to wire them with server-side Command event. On creating RadDock custom controls are loaded and they do postback on click but they don't fire Command event. The problem is also that on postback custom control disappears so one more thing is how to avoid that behavior.

    This is my code:

            private RadDock CreateRadDock()
                int docksCount = CurrentDockStates.Count;
                int idTicket = Convert.ToInt32(rcbTicketi.SelectedValue);

                Ticket ticket = db.Ticket.SingleOrDefault(t => t.idTicket == idTicket);

                RadDock dock = new RadDock();
                dock.DockMode = DockMode.Docked;
                dock.UniqueName = Guid.NewGuid().ToString().Replace("-", "a");
                dock.ID = string.Format("RadDock{0}", dock.UniqueName);
                dock.Title = ticket.idNadredeniTicket + "-" + ticket.RedniBroj + " (" + ticket.Firma.Naziv + ")";
                dock.Text = ticket.Opis;
                dock.Width = Unit.Pixel(500);
                dock.Command += new DockCommandEventHandler(RadDock_Command);

                DockCommand close = new DockCommand();
                close.AutoPostBack = true;
                close.Text = "Zatvori incident";
                close.Name = "Zatvori";

                DockCloseCommand hide = new DockCloseCommand();
                close.AutoPostBack = true;
                close.Text = "Sakrij";

                dock.Commands.Add(new DockExpandCollapseCommand());

                return dock;

            protected void RadDock_Command(object sender, Telerik.Web.UI.DockCommandEventArgs e)
                string naziv = e.Command.Name;

                if (e.Command.Name == "Zatvori")
                else if (e.Command.Name == "Close")
                    Response.Redirect(Request.RawUrl + "?id=1");

    There is also one more thing that bothers me. How could I get the ID of the RadDock on fired event?


  2. Answer
    Slav avatar
    1359 posts

    Posted 14 Mar 2012 Link to this post

    Hi Hrvoje,

    I would suggest setting the RadDock's property CommandAutoPostBack to true in order to ensure that all commands will initiate postback and that the server-side event Command will be handled:
    dock.Width = Unit.Pixel(500);
    dock.Command += new DockCommandEventHandler(RadDock_Command);
    dock.CommandsAutoPostBack = true;

    Also, when the RadDock controls are recreated after a postback, their ID and UniqueName should be the same. If you are using the provided method for every RadDock recreation, each time the value of the properties ID and UniqueName will be different, the dock control will not be reloaded properly and the Command event will not be handled. My suggestion is to either use a specific id for every RadDock, so that you can set it when the control is recreated or if the id is generated randomly to save its value so that you can use it later.

    Incorrect recreation of RadDock could also lead to loosing its content, as in your case. Note that you should add the content of a dock control every time it is dynamically created as the content is not automatically persisted through postback. I would recommend checking the online demos Dock / My Portal and Dock / Dynamically Created Docks as they are good examples for understanding the approach for programmatically adding RadDocks on the page, persisting their state and content. The help article Dynamically Creating RadDock Controls is also quite useful, as it describes in detail the steps that need to be executed in order to create and persist dock controls on the page.

    As for accessing the ID of a RadDock on event, fired by a dock control, you can cast the sender parameter of the handler to a RadDock type and then you can get the ID, as shown in the following code sample:
    protected void RadDock_Command(object sender, Telerik.Web.UI.DockCommandEventArgs e)
        RadDock dock = sender as RadDock;
        string dockID = dock.ID;

    All the best,
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top