Dynamically-created RadButtons not firing server-side OnClick

3 posts, 1 answers
  1. Mike
    Mike avatar
    20 posts
    Member since:
    Jan 2011

    Posted 07 Nov 2011 Link to this post

    Hi there,

    On an ajaxified button press, I'm adding dynamically-created RadButtons to an asp:Panel control contained within a RadAjaxPanel on a ASP.Net page using a Master Page but no RadAjaxManager.

    foreach (CrmSAWorkflowButton wfb in workflowButtons)
        RadButton rb = new RadButton();
        rb.ID = "uxWfb" + wfb.UniqueName;
        rb.CausesValidation = false;
        rb.CssClass = "smallSpaceRightAndBelow";
        rb.CommandArgument = wfb.ID.ToStringGuid();
        rb.Text = wfb.Title;
        rb.Click += new EventHandler(rb_Click);

    However, when I click the dynamically-created buttons, the server-side postback event does not fire, which I kinda need.

    I have tried "ajaxifying" the asp:Panel by adding a RadAjaxManager and adding a new AjaxSetting for the dynamically-created button which updates the asp:Panel, but to no avail: I click the button once, no server-side event and the button stays on the page. I click it again, still no server-side event, the button disappears from the page.

    I've also tried using the AjaxRequest method on the AjaxPanel via OnClientClicked on the dynamically-created button, but that (oddly) blows away some underlying data so that's no good.

    Any thoughts?
  2. Answer
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 08 Nov 2011 Link to this post

    Hello Mike,

    I am not sure in which event you created the RadButton. I tried the same in Page_Load event. Here is the sample code.

    protected void Page_Load(object sender, EventArgs e)
       RadButton RadButton1= new RadButton();
       RadButton1.ID = "RadButton1";
       RadButton1.Text = "Click";
       RadButton1.Click += new EventHandler(rbtn_Click);
    void RadButton1_Click(object sender, EventArgs e)
       //throw new NotImplementedException();

  3. Mike
    Mike avatar
    20 posts
    Member since:
    Jan 2011

    Posted 08 Nov 2011 Link to this post

    Ah, good point - the add method was being called post-Page_Load, but your code in the page load gets fired every time, which is the price you pay for dynamic controls ;-)

    As you've correctly pointed out, the adding of the controls in the Page_Load is the key here (something I figured out at 5pm or so). I re-add the controls back in if need be, which then enables the event to fire.

    Thank you for the reply,

    Mike Kingscott

Back to Top