动态地将控件添加到更新面板

编程入门 行业动态 更新时间:2024-10-22 09:42:39
本文介绍了动态地将控件添加到更新面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在创建一个动态Web应用程序.控件是完全从背后的代码创建的.对于页面中的几个按钮,我想回发,因此我将它们动态地添加到了updatepanel中.但是当我单击按钮时,整个页面都会回发,并且click事件似乎从未触发过. 以下是我使用的代码.请让我知道我做错了吗?

Hi, I am creating a dynamic web application.The controls are completely created from code behind.For few buttons in my page i want post back, so i added them to updatepanel dynamically.But when i click the button the entire page post backs and also the click event never seems to be firing. Below is the Code i used.please let me know what i am doing wrong?

protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { DrawBoxes(); } } public void DrawBoxes() { for (int i = 0; i < 10; i++) { TextBox txtBx = new TextBox(); txtBx.ID = i.ToString(); this.Form.Controls.Add(txtBx); } for (int i = 0; i < 10; i++) { Button bt = new Button(); bt.ID = "dymBtn" + i; bt.Text = "Click Me"; bt.Click += new EventHandler(ClickDel); UpdatePanel updPanel = new UpdatePanel(); updPanel.ID = "upd" + i; updPanel.UpdateMode = UpdatePanelUpdateMode.Conditional; updPanel.ContentTemplateContainer.Controls.Add(bt); Page.Form.Controls.Add(updPanel); } } protected void ClickDel(object sender, EventArgs e) { Button objbtn = (Button)sender; HttpContext.Current.Response.Write(objbtn.Text + "Hellllo"); }

推荐答案

嗨 根据您的要求尝试此代码,它必须对您有帮助 Hi try this code for your requirement it must help you protected Button btn1 = new Button(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { } } override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } private void InitializeComponent() { btn1.Text = "click to change"; btn1.Click += new EventHandler(btn1_Click); this.form1.Controls.Add(btn1); this.Load += new System.EventHandler(this.Page_Load); } void btn1_Click(object sender, EventArgs e) { resdiv.InnerHtml = "clicking "; }

我希望它也对您有用

I hope it works for you also

更多推荐

动态地将控件添加到更新面板

本文发布于:2023-11-06 13:44:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563854.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   面板   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!