1

On dynamically created table based on some condition i've added imagebutton dynamically. parallelly i want to add event for this image button click but the click event is not getting fired. here is my code snippet.

    private void createTable()
    {
        TableRow tableRow = new TableRow();
        TableCell ImageCell = new TableCell();
        ImageButton imgBtndeleteAttr = new ImageButton();
        imgBtndeleteAttr.ID = "imgbtn_" + i.ToString() + j.ToString();
        imgBtndeleteAttr.CssClass = "deleteDynamic";
        imgBtndeleteAttr.OnClientClick = "javascript:return confirm('Do you want to delete this Attribute?');";
        imgBtndeleteAttr.Click += new System.Web.UI.ImageClickEventHandler(imgBtndeleteAttr_Click);
        ImageCell.Controls.Add(imgBtndeleteAttr);
        tableRow.Cells.Add(ImageCell);
    }

and here is the event

    protected void imgBtndeleteAttr_Click(object sender, ImageClickEventArgs e)
    {

        ClientScript.RegisterClientScriptBlock(this.GetType(), "lnk",
           "<script type = 'text/javascript'>alert('Image button Clicked');</script>");
    }
3
  • 1
    Do you call createTable() on the Page.IsPostBack? All dynamically created controls must be recreated on the post-back, otherwise they don't exist and the event handler isn't created Commented Oct 28, 2015 at 9:54
  • no. i cant call the createTable() on the Page.IsPostBack or page load becouse the table and controlls want to generate based on user select parameter Commented Oct 28, 2015 at 10:08
  • If there will only ever be one single dynamic item, then just create it normally and set Visible="false" in the markup - then you can change it in the code to .Visible = true; when required. If there are multiple, then you're in a world of pain that I don't have time to explain Commented Oct 28, 2015 at 12:38

1 Answer 1

1

Its a bad idea to use dynamic controls in asp. The controls have to be recreated on postback. So if you create a button dynamically and click it, postback happens and event is triggered but due to postback the button which triggered the event is destroyed and the raised event is discarded.

Sign up to request clarification or add additional context in comments.

4 Comments

Then may i know how to handle this type of process in asp.net. if we want to create and implement some dynamic control means what is the best way to achieve it
there is no clean way to handle this. you will have to create the control before hand and set Visible=false. else if the condition to create the buttons is data driven i would use a datalist
In my case data driven is not possible. also the datalist too. i cant hide the controlls in pageload. becouse it should generate based on user input. if there is any other way to do this.?
You can create a datatable with one column and 1 row. put any value into it . then bind it with datalist which contains the button. your button is created and you can make it do what ever you want. This is what i actually did to create a dynamic button

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.