3

I have a user control, which is added to another user control. The nested user control is built up of a GridView, an image button and a link button. The nested user control is added to the outer control as a collection object based upon the results bound to the GridView.

The problem that I have is that my link button doesn't work. I click on it and the event doesn't fire. Even adding a break point was not reached. As the nested user control is added a number of times, I have set image button to have unique ids and also the link button. Whilst image button works correctly with its JavaScript. The link button needs to fire an event in the code behind, but despite all my efforts, I can't make it work. I am adding the link button to the control dynamically. Below is the relevant code that I am using:

public partial class ucCustomerDetails : System.Web.UI.UserControl
{
public event EventHandler ViewAllClicked;

protected override void CreateChildControls( )
{
   base.CreateChildControls( );

   string strUniqueID = lnkShowAllCust.UniqueID;
   strUniqueID = strUniqueID.Replace('$','_');
   this.lnkShowAllCust.ID = strUniqueID;
   this.lnkShowAllCust.Click += new EventHandler(this.lnkShowAllCust_Click);
   this.Controls.Add(lnkShowAllCust);
}

protected override void OnInit (EventArgs e)
{
   CreateChildControls( );
   base.OnInit(e);
}

protected override void OnLoad(EventArgs e)
{
   base.EnsureChildControls( );
}

protected void Page_Load(object sender, EventArgs e)
{
   if (IsPostBack)
   {
   CreateChildControls( );
   }
}

protected void lnkShowAllCust_Click(object sender, EventArgs e)
{
   this.OnCustShowAllClicked(new EventArgs ( ));
}

protected virtual void OnCustShowAllClicked(EventArgs args)
{
   if (this.ViewAllClicked != null)
   {
      this.ViewAllClicked(this, args);
   }
}    
}

I have been stuggling with this problem for the last 3 days and have had no success with it, and I really do need some help.

Can anyone please help me?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
user267669
  • 31
  • 1
  • 2

5 Answers5

2

My LinkButton wasn't firing it's Click event, and the reason was I had its CausesValidation property set to True. If you don't want the link to validate the form, be sure to set this to False.

kad81
  • 10,712
  • 3
  • 38
  • 44
  • I was having a very similar issue. I had a user control that had a link button in it. This user control was being used on multiple pages and was working flawlessly on every page except for 1. I added the CausesValidation="false" to the link button and it now works on the page it wasn't. – Allen May 26 '16 at 20:34
1

Try adding your click event to the linkbutton tag:

<asp:LinkButton runat="server" OnClick="linkShowAllCust_Click" />

Or adding it to your Page_Load:

Page_Load(object sender, EventArgs e) 
{
  this.lnkShowAllCust.Click += new EventHandler(this.lnkShowAllCust_Click);
}
Charlie
  • 10,227
  • 10
  • 51
  • 92
  • Hi I know I should have added the markup, but on my markup I have already added the server tag for the link button. Because I am using protected override OnLoad method, the Page_Load is never fired. So, this event never gets kicked off. – user267669 Feb 06 '10 at 10:39
  • 1
    Calling base.OnLoad(sender, e) should help after your call to EnsureChildControls(); inside your overridden OnLoad() – Dimi Takis Feb 06 '10 at 10:44
  • @andyriome: Call `base.OnLoad(e);` in your `override void OnLoad(EventArgs e)` is what you need! – abatishchev May 16 '10 at 15:18
  • I think it's better to register additional event handlers in a constructor rather in `Page_Load` – abatishchev May 16 '10 at 15:20
0

I had the same issue. I had viewstate="false" on the page I was adding the control to. (on the aspx page)

calorie712
  • 348
  • 4
  • 14
0

Is the usercontrol within the gridview? If so register the event handler on the gridview's onrowcreated event.

FiveTools
  • 5,970
  • 15
  • 62
  • 84
0

It appears that you have a viewstate issue. Because the control isn't there when the viewstate is loaded the application doesn't know how to hook up the event to be fired. Here is how to work around this.

You can actually make your app work like normal by loading the control tree right after the loadviewstateevent is fired. if you override the loadviewstate event, call mybase.loadviewstate and then put your own code to regenerate the controls right after it, the values for those controls will be available on page load. In one of my apps I use a viewstate field to hold the ID or the array info that can be used to recreate those controls.

Protected Overrides Sub LoadViewState(ByVal savedState As Object)
    MyBase.LoadViewState(savedState)
    If IsPostBack Then
        CreateMyControls()
    End If
End Sub
Middletone
  • 4,190
  • 12
  • 53
  • 74
  • Is there anyything else I need to do on Page_Load? The only reason why I say this is because I am using override OnLoad method, and I have noticed whilst in debug mode this isn't really working correctly. Correctly, if I am wrong that isn't OnLoad supersedes Page_Load? – user267669 Feb 06 '10 at 22:18