Link button on ASP.NET user control not working

I have a user control that is being added to another user control. The nested user control is created from a GridView, an image button, and a link button. The nested user control is added to the external control as a collection object based on the results associated with the GridView.

The problem is that my link button does not work. I click on it and the event does not fire. Even adding a breakpoint has not been reached. Since the nested user control has been added several times, I set the image button to have unique identifiers as well as a link button. So far, the image button is working correctly with its JavaScript. The link button should trigger an event in the code behind, but despite all my efforts, I cannot get it to work. I add a 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 dealing with this problem for the last 3 days and have not had success with it, and I really need help.

Can anybody help me?

+3
5

My LinkButton Click, , CausesValidation True. , , False.

+2

click linkbutton:

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

Page_Load:

Page_Load(object sender, EventArgs e) 
{
  this.lnkShowAllCust.Click += new EventHandler(this.lnkShowAllCust_Click);
}
+1

gridview? , gridview onrowcreated event.

0

, . , viewstate, , , . .

, loadviewstateevent. loadviewstate, mybase.loadviewstate, , . viewstate , .

Protected Overrides Sub LoadViewState(ByVal savedState As Object)
    MyBase.LoadViewState(savedState)
    If IsPostBack Then
        CreateMyControls()
    End If
End Sub
0

. viewstate = "false" , . ( aspx)

0

Source: https://habr.com/ru/post/1731666/


All Articles