views:

818

answers:

3

Hi

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?

A: 

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
HiI 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.
Calling base.OnLoad(sender, e) should help after your call to EnsureChildControls(); inside your overridden OnLoad()
Greco
@andyriome: Call `base.OnLoad(e);` in your `override void OnLoad(EventArgs e)` is what you need!
abatishchev
@Charlie: I think it's better to register additional event handlers in a constructor rather in `Page_Load`
abatishchev
A: 

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

FiveTools
A: 

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
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?