0

I'm trying to do the following: I have only a ScriptManager and an UpdatePanel (with empty ContentTemplate) on a page. I would like to add a button on it during the page load. Then after the button is pressed I would like to get an Ajax request and generate some dynamic controls on the page.

But it doesn't work, the code is below:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
        listServices();
}

private void listServices()
{
    Button button = new Button();
    button.Text = "Save";
    button.ID = "Save";
    button.Click += new EventHandler(button_Click);

    UpdatePanel1.ContentTemplateContainer.Controls.Clear();
    UpdatePanel1.ContentTemplateContainer.Controls.Add(button);

    AsyncPostBackTrigger trig = new AsyncPostBackTrigger();
    trig.ControlID = button.UniqueID;
    trig.EventName = "Click";
    UpdatePanel1.Triggers.Add(trig);
}

void button_Click(object sender, EventArgs e)
{
    throw new NotImplementedException();
}

I've looked into: [Adding controls dynamically to an UpdatePanel in ASP.NET AJAX][1] But it doesn't solve the problem in my case.

Doing this must be possible, many services have this functionality.

Thanks for help. Adam

Adam
  • 756
  • 4
  • 10
  • 23
  • You haven't really detailed what the problem is... "it doesn't work" doesn't give us much to go on. – womp Dec 15 '09 at 22:00
  • OK, the problem is: When the page is generated I see the Save button. But when I click it, the button_Click event is not triggered (but the Page_Load method is, IsPostBack==false). – Adam Dec 15 '09 at 22:10
  • hai adam !Ispostback was the prob... – ACP Dec 16 '09 at 03:02

1 Answers1

0

Hai adam, Have a look at this one How can I create buttons and hook up events from postback

It deals with commandEventHandler and you change it to event handler and it may work for you...

Community
  • 1
  • 1
ACP
  • 34,682
  • 100
  • 231
  • 371