创建ASP.NET按钮动态使用Ajax调用按钮、动态、ASP、NET

2023-09-10 21:46:42 作者:最宝贝你

我想,当用户进行Ajax调用我的服务器页面(另一asp.net页面),我Wnt信号做一些关于这个按钮的按钮单击事件来创建一个asp.net按钮控制/链接按钮控制。如何做到这一点?我是否需要使用代理?

I want to create a asp.net button control / link button control when user makes an ajax call to my server page (another asp.net page) and I wnt to do something on the button click event of this button. How to do this ? Do i need to use delegate ?

推荐答案

没有,你并不需要一个委托这一点,它会为你创建。在您的AJAX回调,你应该做这样的事情:

No, you don't need a delegate for this, it will be created for you. In your AJAX callback you should do something like this:

Button btn = new Button();
btn.Click += MyOnClickMethod;     // must use +=, not =
btn.Text = "click me";
myUpdatePanel.Controls.Add(btn);  // the AJAX UpdatePanel that you want to add it to
myUpdatePanel.Update();           // refresh the panel

的方法,MyOnClickMethod必须具有相同的签名作为一个正常的点击处理程序。像这样的东西会做:

The method MyOnClickMethod must have the same signature as a normal Click handler. Something like this will do:

protected void MyOnClickMethod(object sender, EventArgs e)
{ 
   // do something
}

这就是它。这里涉及到动态控制许多错综复杂,但基础是为布局上面。

that's about it. There are many intricacies involved with dynamic controls, but the basis is as laid out above.