Dynamically created image button click event is not fired - c#

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>");
}

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.

Related

C# Dynamic RadioButton in Update Panel is not firing on first click?

I have a number of radio buttons created dynamically inside a loop and added to a div inside update panel. On each postback request triggered by checked change event, I recreate the radio buttons in my page_init method. My problem is the radio button I selected is not checked and the checked changed event is not firing on first click. But on subsequent clicks, it works normally and the checked changed event is fired. Only the first click is not firing. What could be the issue?
Simple dynamic radio button.
RadioButton btn2 = new RadioButton();
btn2.Text = "TEST";
btn2.CheckedChanged += Btn2_CheckedChanged; ;
btn2.AutoPostBack = true;
pricetbldiv.Controls.Add(btn2);
private void Btn2_CheckedChanged(object sender, EventArgs e)
{
RadioButton btn = (RadioButton)sender;
string text = btn.Text;
}
Try to assign group and ID
btn2.ID = "Text";
btn2.Text = "Text";
btn2.GroupName = "RB";
btn2.CheckedChanged += new EventHandler(Btn2_CheckedChanged);

Dynamically added Link Button doesn't fire the on click event on the first click - C#

I am dynamically adding Link Buttons to my Gridview as seen below:
protected void addLinks()
{
foreach (GridViewRow gvr in gvData.Rows)
{
if (gvr.RowType == DataControlRowType.DataRow)
{
string itemNbr = gvr.Cells[1].Text;
LinkButton lb = new LinkButton();
lb.Text = itemNbr;
lb.Click += genericLinkButton_Click;
foreach (Control ctrl in gvr.Cells[1].Controls)
{
gvr.Cells[1].Controls.Remove(ctrl);
}
gvr.Cells[1].Controls.Add(lb);
}
}
}
This addLinks() function is called in my gridview_RowDataBound event and the Page Load event if(isPostPack).
The problem is that when I click the link buttons, the genericLinkButton_Click event does not get fired on my first click. It causes a postback, and then if I click it again, or click one of the other Link Buttons, the genericLinkButton_Click event is fired.
How can I make sure the click event happens on my first click?
Thanks!
My condolences for having to bother with WebForms.
When using Webforms and dynamically creating controls you are required to assign an ID to the created control before adding the control to the tree in order for them to work properly. Otherwise they will have changing ID's during the pagelifecycle resulting in the described behaviour.
private int _runningIndex = 0;
protected void gvData_RowCreated(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string itemNbr = e.Row.Cells[1].Text;
LinkButton lb = new LinkButton();
lb.ID = "btn" + (_runningIndex++).ToString();
lb.Text = itemNbr;
lb.Click += genericLinkButton_Click;
foreach (Control ctrl in e.Row.Cells[1].Controls)
{
e.Row.Cells[1].Controls.Remove(ctrl);
}
e.Row.Cells[1].Controls.Add(lb);
}
}
should be working.
RowDataBound is triggered only if the GridView gets databound, so when you call gvData.DataBind(). But dynamically created controls must be created again on every postback.
The most appropriate event to create controls dynamically in a GridView is RowCreated which is triggered on every postback. Note that you the GridViewRow's DataItem is null on postback. So you cannot access it's datasource as opposed to RowDataBound. But that seems not to be necessary anyway here.
Note also that you don't need to loop all rows in RowDataBound or RowCreated since these events are triggered for every row in the GridView anyway.
protected void gvData_RowCreated(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string itemNbr = e.Row.Cells[1].Text;
LinkButton lb = new LinkButton();
lb.Text = itemNbr;
lb.Click += genericLinkButton_Click;
foreach (Control ctrl in e.Row.Cells[1].Controls)
{
e.Row.Cells[1].Controls.Remove(ctrl);
}
e.Row.Cells[1].Controls.Add(lb);
}
}

Adding Triggers dynamically on UpdatePanel for dynamially added controls

I am Adding array Buttons to a simple panel dynamically which is located in an Update Panel, now I want to Add triggers for UpdatePanel on click event of these buttons. My codes is as below:
protected void AddButtons()
{
Button[] btn = new Button[a];
for (int q = 0; q < a; q++)
{
btn[q] = new Button();
buttonsPanel.Controls.Add(btn[q]);
btn[q].ID = "QID" + q;
btn[q].Click += new EventHandler(_Default_Click);
btn[q].Attributes.Add("OnClick", "Click(this)");
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = btn[q].ID;
trigger.EventName = "Click";
UpdatePanel2.Triggers.Add(trigger);
}
}
Now click event is not fired when i click on any of these bottons and buttons are getting removed.
Please note that these buttons are not available on Page_Init() method.
You need to assign UniqueID instead of ID to AsyncPostBackTrigger.ControlID property. Try to use the following code:
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = btn[q].UniqueID;
trigger.EventName = "Click";
UpdatePanel2.Triggers.Add(trigger);
I came across this post when I was attempting to dynamically add triggers to an update panel which contained a gridview. I have buttons in the gridview and defining the trigger in the page doesn't work as a unique ID for each button is generated when each row is created.
Generating the trigger like such;
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = btn[q].UniqueID;
trigger.EventName = "Click";
UpdatePanel2.Triggers.Add(trigger);
did not work for me. The control could not be found, however when using the RegisterPostbackControl or the RegisterAysncPostbackControl commands it worked.
The end example is as follows;
protected void BankAccountDocumentGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton linkButton = (LinkButton)e.Row.Cells[3].FindControl("DocumentsDownloadButton");
ScriptManager.GetCurrent(Page).RegisterPostBackControl(linkButton);
}
}
I figured that the original poster or others who come across this post may benefit from my findings.

Adding controls to a table control dynamically

I have one table control "table1"
And added controls to it in click event of one button as :
protected void Button2_Click(object sender, EventArgs e)
{
TableRow row;
TableCell cell;
for (int i = 0; i < 3; ++i)
{
TextBox txt = new TextBox();
txt.Text = i.ToString();
row = new TableRow();
cell = new TableCell();
cell.Controls.Add(txt);
row.Controls.Add(cell);
Table1.Controls.Add(row);
}
}
but i cant retrieve this controls in click event of another button. i think it is because of postback.
How can i prevent it?
When adding controls dynamically, if you want to access them on postback, you need to re-create them every time the page is loaded.
Adding them in the click handler of one button and expecting them to be there for the click handler of another button will not work, as they have not been re-created on the second postback.
You need to understand the asp.net page life cycle and change the logic of your page to fit in with it.

asp.net - Button event does not fire up

I have a search button on my page that runs a query on a DB, pulls out and displays some entries in a table, and for each entry I create a button. It looks something like this:
List<Friend> friends = SearchFriend(searchStr);
foreach (Friend f in friends)
{
TableCell addCell = new TableCell(), nameCell = new TableCell();
addCell.Text = "";
if (!f.IsMyFriend)
{
LinkButton addFriendBtn = new LinkButton();
addFriendBtn.Text = "Add as Friend";
addFriendBtn.Click += new EventHandler(addFriendBtn_Click);
addFriendBtn.ID = "add_" + f.ID.ToString();
addCell.Controls.Add(addFriendBtn);
}
nameCell.Text = f.Name;
TableRow row = new TableRow();
row.Cells.Add(addCell);
row.Cells.Add(nameCell);
SearchFriendTable.Rows.Add(row);
}
Problem is that the LinkButton event does not fire when it is pressed (changing LinkButton to a simple Button does not fix this either).
This is the html that I get in this portion:
<td><a id="ctl00_contentPH_add_2" href="javascript:__doPostBack('ctl00$contentPH$add_2','')">Add as Friend</a></td>
Also - when I put a breakpoint on Page_Load I do see the __EVENTTARGET with this control's id in it - however the event never starts running.
Any clues?
Thanks.
Adding to Himadri's answer:
Dynamically added controls need to be rewired in page init. Then the event will fire. I had a very similar issue Dynamically loaded Controls in a wizard
Where and when did you created that button?
If you dynamically create Buttons and want to listen to a event you have to create that button in the PageInit event. Always! So do not use if(!IsPostback)
Try with this.
<td><a id="ctl00_contentPH_add_2" href="javascript:__doPostBack('<%=ct100_contentPH_add_2.ClientId %>','')">Add as Friend</a></td>
Add event handler for the link buttons and handle those events.
if (!f.IsMyFriend)
{
LinkButton addFriendBtn = new LinkButton();
addFriendBtn.Text = "Add as Friend";
addFriendBtn.Click += new EventHandler(addFriendBtn_Click);
addFriendBtn.ID = "add_" + f.ID.ToString();
addFriendBtn.Click += new EventHandler(addFriendBtn_Click);
addCell.Controls.Add(addFriendBtn);
}
the event:
protected void addFriendBtn_Click(object sender, EventArgs e)
{
LinkButton lnk = (LinkButton)sender;
// do your coding
}

Categories