开发者

Link Button on ASP.NET user control not firing

开发者 https://www.devze.com 2022-12-19 14:47 出处:网络
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 contr

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 CreateChildCon开发者_开发百科trols( )
{
   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?


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.


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


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消