I am programatically making some LinkButtons, and they worked fine for a while, but now the events aren’t firing and I can’t figure out why?
This is what makes the button:
protected void MakeUploadButton(attachment a, PlaceHolder ph) { LinkButton lb = new LinkButton() { Text = 'Upload New ' + a.attachment_type.type_name, CssClass = 'button right', ID = 'lb' + a.attachment_type.file_tag, CommandArgument = a.attachment_type_id.ToString(), CommandName = a.attachment_type.type_name, CausesValidation = false }; lb.Click += new EventHandler(showModalPopup); lb.DataBind(); ph.Controls.Add(lb); }
ShowModalPopup exists and all, but when I run it in Debug, nothing inside of it ever fires… for some reason, it is not getting called. Any ideas?
found my answer here: http://bytes.com/groups/net-asp/329287-linkbutton-event-not-firing
It’s because .NET nukes an elements event handlers on postback if they aren’t set in viewstate. All you need to do is re-attach the eventhandler in the onload event.