Click here to Skip to main content
14,644,129 members
Rate this:
Please Sign up or sign in to vote.
See more:
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");
}
Posted
Updated 16-Sep-11 3:12am
v3

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100