Skip to main content
 首页 » 编程设计

c#之如何根据用户请求调用 WebUserControls

2024年01月11日33me-sa

我正在构建一个网站,我需要根据用户请求调用 WebUserControls (.ascx),我该如何实现?这可能吗?

例子:

protected void userclick_click(object sender, EventArgs e) 
{ 
    if(textbox1.Text == "2") 
    { 
        call WebUserControl1.ascx 
    } 
    else 
    { 
      /*do nothing*/ 
    } 
} 

我为此使用 C#。

提前谢谢你。

请您参考如下方法:

protected void userclick_click(object sender, EventArgs e) 
{ 
    if(textbox1.Text == "2") 
    { 
        Control c1 = LoadControl("MyUserControl.ascx"); 
        //page or whatever control you want to add to 
        Page.Controls.Add(c1); 
    } 
    else 
    { 
      /*do nothing*/ 
    } 
}