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
}
Related
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.
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);
}
}
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.
I am creating linkbuttons inside of a panel and hooking up an event handler to the Click event. It isn't firing. This is my code. Is this lost on postback?
foreach (var item in clients)
{
var codeLb = new LinkButton() { Text = item.CLIENT_CODE, CssClass = "codeColumn", CommandArgument = item.CLIENT_CODE, CommandName = "Select" };
codeLb.Click += ResultsLinkButton_Click;
ResultsPanel.Controls.Add(codeLb);
var nameLb = new LinkButton() { Text = item.CLIENT_NAME, CssClass = "nameColumn", CommandArgument = item.CLIENT_CODE, CommandName = "Select" };
nameLb.Click += ResultsLinkButton_Click;
ResultsPanel.Controls.Add(nameLb);
}
Yes every dynamically created controls are lost on after postback.
You need to recreate them again on postback and again hook up the Event Handler.
You can read more
https://web.archive.org/web/20210330142645/http://www.4guysfromrolla.com/articles/092904-1.aspx
Dynamic Controls and Postback
Hope it helps.
In my sharepoint web-part application. I am dynamically generating LinkButtons as below. and this works fine
foreach (var pName in productTypesNames[productType] )
{
var subLi = new HtmlGenericControl("li");
var linkButton = new LinkButton{ Text = pName };
linkButton.Click += new EventHandler(linkButton_Click);
subLi.Controls.Add(linkButton);
ul.Controls.Add(subLi);
}
However, when I click on one of the links in UI, my debugger never hits the breakpoint that is set at very first line of
void linkButton_Click(object sender, EventArgs e)
{
}
More Code
protected void StateClicked(object sender, CommandEventArgs e)
{
//Generate a dictionary of type Dictionary<string, List<string>>
//Display the dictionary
foreach (var productType in productTypesNames.Keys)
{
var li = new HtmlGenericControl("li");
nav.Controls.Add(li);
var ul = new HtmlGenericControl("ul");
var anchor = new HtmlGenericControl("a");
anchor.Attributes.Add("href", "#");
foreach (var pName in productTypesNames[productType] )
{
var subLi = new HtmlGenericControl("li");
var linkButton = new LinkButton{ Text = pName };
linkButton.Click += new EventHandler(linkButton_Click);
subLi.Controls.Add(linkButton);
ul.Controls.Add(subLi);
}
anchor.InnerHtml = productType;
li.Controls.Add(anchor);
li.Controls.Add(ul);
}
}
Where stateClicked is called by a click on the image map of USA.
You probably aren't recreating the dynamically generated links on every postback.
If you have a if (!IsPostback) wrapped around your foreach, try removing it.
i had the same problem here ....
i was creating an HtmlTable after firing an event...
this table has (n) HtmlTableRows (calculated in the event handler)
now each row contains 2 LinkButtons that are generated from the code behind .. after an event is handled...
and each LinkButton is assigned a new event handler:
lnkbtnEdit.CommandArgument = b.BookID.ToString();
lnkbtnEdit.Click += new EventHandler(lnkbtnEdit_Click);
where lnkbtnEdit_Click signature is as follows:
protected void lnkbtnEdit_Click(object sender, EventArgs e)
the weird thing is that .. there is a postback when i click the generated LinkButton... but the event was not firing...
i don't know exactly what the problem was ... but i found the solution: ..
it appears as if these generated controls disappear on the postback (tried to assign an id and used Page.FindControl() witch returned with null !!)
so i had to re-link the buttons.... on the Page_Load i re-generated the LinkButtons, with same ID... and linked them to their respective EventHandlers