How to get handle of dynamic image button in ajax backend
I am writing an image button to a table cell on a new row when the user selects an item in the list:
ImageButton imgbtnRemove = new ImageButton();
imgbtnRemove.ID = "uxStandardLetterDeleteImage_" + items.letterName;
imgbtnRemove.CommandName = "uxStandardLetterDeleteImage_" + items.letterName;
imgbtnRemove.ImageUrl = items.remove;
imgStatus.AlternateText = "Remove";
tRow.Cells[3].Controls.Add(imgbtnRemove);
When a new image is clicked, I cannot process it. I see this in the Page_PreRender event where I also reload the table on every postback.
string returnData = Request.Form.ToString();
but iterating through the form manages the images:
if (c is System.Web.UI.WebControls.Button ||
c is System.Web.UI.WebControls.ImageButton)
does not find it. I can find it if I manually insert:
imgbtnRemove.Click += new System.Web.UI.ImageClickEventHandler(this.ButtonClick);
in Page_Load and then grab it on the click event:
switch (((System.Web.UI.WebControls.ImageButton)sender).CommandName)
...
but as newlines are added and removed, this becomes a rather ugly programmatic way. I think there should be an elegant solution to create and find a dynamic image on the fly from server side code. I've done a lot of digging, but it makes me sick.
Thanks in advance...
a source to share
If you create dynamic controls during the event handling phase of the page lifecycle (for example, on a selected event), then control will be disabled on the next postback.
For dynamic controls to register with ViewState, they must be created during the Init phase of the event lifecycle.
Also, you say that you are iterating with the form controls ... when you add the image button to the table cells. Are you recursively lowering the control hierarchy to look for an image button?
a source to share