在C#asp.net中将用户控件用作动态控件(可能是文字控件)
我有一个用户控件,它是一个图像按钮。 我希望该控件在我的网页中动态添加“n”次。 “n”来自数据库。 我尝试了以下代码: 在“aspx”中:在C#asp.net中将用户控件用作动态控件(可能是文字控件),c#,asp.net,user-controls,C#,Asp.net,User Controls,我有一个用户控件,它是一个图像按钮。 我希望该控件在我的网页中动态添加“n”次。 “n”来自数据库。 我尝试了以下代码: 在“aspx”中: ......................................... //这里有一些代码 ....................................... 在“cs”中: for(int i=0;i发生这种情况是因为您总是将uc\u imgbtn分配给您的new UserControl();。换句话说,您总是将相同的现有控件
.........................................
//这里有一些代码
.......................................
在“cs”中:
for(int i=0;i发生这种情况是因为您总是将uc\u imgbtn
分配给您的new UserControl();
。换句话说,您总是将相同的现有控件分配给plhdr\u seat.Controls
集合
有关更多详细信息,请查看上的注释:
一个控件一次只能分配给一个控件。ControlCollection
如果该控件已经是另一个控件的子控件,则它是
在将该控件添加到另一个控件之前将其从该控件中删除
如果您想每次向plhdr\u座椅控制添加一个新的uc\u imgbtn
控件,您应该将代码更改为:
UserControl uc = new UserControl(); // change this
uc = uc_imgbtn; // remove this
在第一行中,您必须确保正确初始化uc
控件。从代码中我可以看出,这应该可以工作:imgbtn uc=new imgbtn();
不要创建UserControl
,调用Page.LoadControl
方法以获取uc\u imgbutton
的新实例
for(int i = 0; i < 5; i++)
{
UC_imgbutton uc = (UC_imgbutton)LoadControl("~/components/seatfromdb/usercontrol/UC_imgbutton.ascx");
//uc.Attributes.Keys = "~/images/buttonorange.png";
//uc.ID = "uc" + i.ToString();
//uc.Height = 30;
plhdr_seat.Controls.Add(uc);
plhdr_seat.Controls.Add(new LiteralControl("<br />"));
}
for(int i=0;i<5;i++)
{
UC_imgbutton UC=(UC_imgbutton)LoadControl(“~/components/seatfromdb/usercontrol/UC_imgbutton.ascx”);
//uc.Attributes.Keys=“~/images/buttonorange.png”;
//uc.ID=“uc”+i.ToString();
//uc.高度=30;
plhdr_座椅控制添加(uc);
plhdr_seat.Controls.Add(新的LiteralControl(“
”);
}
uc#imgbtn中的uc#imgbtn是什么?我已经编辑了我的问题。请看it@artmImageBtn的Id,我看到的一个用户控件。不要添加5次imgbtn,而是创建一个新的UC\u imgbutton
并添加此新控件。imgbtn UC=new imgbtn();不工作。imgbtn为tagnameLoadControl未突出显示。缺少引用。我有System.Web.UI。但仍然缺少一些内容。他们一定禁止你进入谷歌-
UserControl uc = new UserControl(); // change this
uc = uc_imgbtn; // remove this
for(int i = 0; i < 5; i++)
{
UC_imgbutton uc = (UC_imgbutton)LoadControl("~/components/seatfromdb/usercontrol/UC_imgbutton.ascx");
//uc.Attributes.Keys = "~/images/buttonorange.png";
//uc.ID = "uc" + i.ToString();
//uc.Height = 30;
plhdr_seat.Controls.Add(uc);
plhdr_seat.Controls.Add(new LiteralControl("<br />"));
}