动态地将控件添加到更新面板 [英] Dynamically adding control to an updatepanel

查看:61
本文介绍了动态地将控件添加到更新面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在创建一个动态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


这篇关于动态地将控件添加到更新面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆