我正在构建一个网站,其中我需要根据用户请求调用WebUserControls (.ascx),我如何才能做到这一点?这有可能吗?
示例:
protected void userclick_click(object sender, EventArgs e)
{
if(textbox1.Text == "2")
{
call WebUserControl1.ascx
}
else
{
/*do nothing*/
}
}为此,我使用了C#。
提前谢谢你。
发布于 2011-10-05 09:04:21
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*/
}
}发布于 2011-10-05 09:03:40
您可以调用它并将其附加到PlaceHolder。查看一下-查看http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.placeholder.aspx以获取有关该控件的详细信息。
它可以让你在运行时用任何你喜欢的东西填充PlaceHolder。这样做的好处是,它允许您在设计时将PlaceHolder放在您想要的任何位置,而不仅仅是将其添加到Page.Controls列表中。
protected void userclick_click(object sender, EventArgs e)
{
if(textbox1.Text == "2")
{
WebUserControl1 uc = (WebUserControl1) Page.LoadControl("WebUserControl1.ascx");
PlaceHolder1.Controls.Add(uc);
}
else
{
/*do nothing*/
}
}https://stackoverflow.com/questions/7655615
复制相似问题