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

Dynamically-created RadButtons not firing server-side OnClick

2 Answers 397 Views
Button
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 07 Nov 2011, 05:39 PM
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);
    uxWorkflowButtonsP.Controls.Add(rb);
}

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 Answers, 1 is accepted

Sort by
1
Accepted
Princy
Top achievements
Rank 2
answered on 08 Nov 2011, 06:05 AM
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.

C#:
protected void Page_Load(object sender, EventArgs e)
 {
   RadButton RadButton1= new RadButton();
   RadButton1.ID = "RadButton1";
   RadButton1.Text = "Click";
   RadButton1.Click += new EventHandler(rbtn_Click);
   Panel1.Controls.Add(rbtn);
 }
void RadButton1_Click(object sender, EventArgs e)
 {
   //throw new NotImplementedException();
 }

Thanks,
Princy.
0
Mike
Top achievements
Rank 1
answered on 08 Nov 2011, 09:21 AM
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

Tags
Button
Asked by
Mike
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Mike
Top achievements
Rank 1
Share this question
or