Web用户控制按钮单击
我有一个 webusercontrol ( MiniUrunControl.ascx ),在这个 webusercontrol 中我添加了一个按钮,按钮单击功能是 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);
}
}
好的,当运行网站时,我看到我的所有产品(面板中的 10 个 webusercontrols)都添加在面板控件中。但是当我单击 imgButtonMini 按钮时,所有控件都消失了并且面板为空。我的按钮点击功能也不起作用。
你能帮我看看问题是什么以及如何运行按钮功能吗? 谢谢
I have a webusercontrol ( MiniUrunControl.ascx ) and in this webusercontrol I add a button and button click function is 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[] { });
}
Now, I add a panel control in Default.aspx and Default.aspx CodeBehind like this;
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 When run web site , I see all my products ( 10 webusercontrols in panel ) which are added in panel Control. But When I click imgButtonMini button , All control gone and panel is empty. Also my Button Click function doesnot works.
Can you help me what is the problem and How can I run button function ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
迭代主控件中的每个控件,例如您有 Product.ascx 控件和
在您的product_page.aspx 中,您有一个包含
id="pcdiv"
和runat="server"
的 div。现在在 C# 中以这种方式迭代:请检查控制转换,因为我还没有测试此代码,但我在某些项目中使用了此 1。
iterate each control in main control for example you have product.ascx control and
in your product_page.aspx you have a div having
id="pcdiv"
andrunat="server"
. now in C# iterate this way:please check control casting as I haven't test this code but I used this 1 in some project.
阅读您的代码,您只需将子控件添加到页面一次。如果您想使用此方法,则每次回发时也需要添加它们,否则控件将消失。
Reading your code you are only adding the child controls once to the page. They need to be added every time you postback as well if you want to use this approach otherwise the controls will disappear.