我有一个and控件( MiniUrunControl.ascx ),在这个and控件中我添加了一个按钮,按钮点击函数是imgButtonMini_Click。
protected void imgButtonMini_Click(object sender, ImageClickEventArgs e)
{
DataTable _tablo = (DataTable)Session["KULLANICISEPETI"];
foreach (DataRow _row in _tablo.Rows)
{
if (_row["urunid"].ToString() == lbUrunID.Text)
{
_tablo.Rows.Remove(_row);
Session["KULLANICISEPETI"] = _tablo;
break;
}
}
this.Page.GetType().InvokeMember("ShowSepetBilgisi",
System.Reflection.BindingFlags.InvokeMethod, null, this.Page, new object[] { });
}现在,我在Default.aspx和Default.aspx CodeBehind中添加了一个面板控件,如下所示;
protected void Page_Load(object sender, EventArgs e)
{
if(ispostback==false) ShowSepetBilgisi();
}
public void ShowSepetBilgisi()
{
DataTable _tablo = (DataTable)Session["KULLANICISEPETI"];
if (_tablo == null) return;
pnlMiniUrunler.Controls.Clear();
foreach (DataRow _row in _tablo.Rows)
{
MiniUrunControl _mini = (MiniUrunControl)LoadControl("MiniUrunControl.ascx");
_mini.SetInfo(_row["urunid"].ToString(), _row["adet"].ToString());
pnlMiniUrunler.Controls.Add(_mini);
}
}Ok当运行网站时,我看到我的所有产品(面板中的10个web控件)都添加到面板控件中。但是当我点击imgButtonMini按钮时,所有的控件都消失了,面板是空的。另外,我的Button Click功能也不起作用。
你能告诉我有什么问题吗?我该如何运行按钮功能?谢谢
发布于 2012-07-29 21:44:05
迭代主控件中的每个控件,例如,你有product.ascx控件,而在你的product_page.aspx中,你有一个包含id="pcdiv"和runat="server"的div。现在在C#中这样迭代:
foreach(control item in (product)div.controls.oftype(product)){
if(item.value == 'true') {
Response.write("Got it"); break;//incase only 1 object to iterate
}
}请检查控制投射,因为我没有测试这段代码,但我在一些项目中使用了这1。
发布于 2012-02-07 20:18:05
读取代码时,您只能将子控件添加到页中一次。如果你想使用这种方法,它们也需要在你每次回发时添加,否则控件将会消失。
https://stackoverflow.com/questions/9174837
复制相似问题