ASP.NET在Ajax请求中将Ajax控件添加到UpdatePanel

时间:2022-07-02 16:49:58

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.

我正在尝试执行以下操作:我在页面上只有一个ScriptManager和一个UpdatePanel(带有空的ContentTemplate)。我想在页面加载期间在其上添加一个按钮。然后在按下按钮后,我想获得Ajax请求并在页面上生成一些动态控件。

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.

我已经研究过:[动态地将控件添加到ASP.NET AJAX中的UpdatePanel] [1]但是它并没有解决我的问题。

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

必须这样做,许多服务都具有此功能。

Thanks for help. Adam

感谢帮助。亚当

1 个解决方案

#1


0  

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

Hai亚当,看看这个我如何创建按钮并从回发中连接事件

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

它处理commandEventHandler并将其更改为事件处理程序,它可能适合您...

#1


0  

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

Hai亚当,看看这个我如何创建按钮并从回发中连接事件

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

它处理commandEventHandler并将其更改为事件处理程序,它可能适合您...