在ASP.NET中添加用户控制动态 [英] Adding User Control Dynamically in ASP.NET
问题描述
我知道这个问题被问了很多次,但还是我的问题不解决,
I know this question was asked many times, but still my problem is not solved,
我试图遍历对象的列表上,并填写模板用户控制与该对象,
搜索像结果列表。
I'm trying to iterate on a list of objects, and fill a template "user control" with that object, like result list of a search.
在这些用户控件存在应重定向到另一页一个LinkButton,
当我点击该LinkButton的什么也没有发生,我GOOGLE了它,但没有令人满意的答案。
in these user controls there is a linkButton which should redirect to another page, when I click on that linkButton nothing happens, I googled it but no satisfying answer.
这里是code,我将与按钮,而不是用户控件说明:
here is the code, I'll illustrate with button instead of a user control:
protected override void OnInit(EventsArgs e)
{
for(int i=0;i<10;i=i+1)
{
Button b = new Button();
b.ID = "Button" + i;
b.Click += new System.EventHAndler(this.Button_OnClick);
Controls.Add(b);
}
base.OnInit(e);
}
private void Button_OnClick(object Sender,System.EventsArgs e)
{
Response.Redirect("~/Some.aspx");
}
public override void VerifyRenderingInServerForm(Control control)
{
return;
}
它永远不会调用Button_OnClick方法。
It never calls the Button_OnClick method.
先谢谢了。
推荐答案
按钮将永远不会触发因为不是服务器表单控件的子项。
The button will never fire because is not a child of a server form control.
如果没有表单控件的是,你需要添加:
If there is no form control yet, you need to add it:
<form id="form1" runat="server">
</form>
和替换
Controls.Add(b);
通过
form1.Controls.Add(b);
这篇关于在ASP.NET中添加用户控制动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!