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?

4

5 回答 5

2

我的 LinkBut​​ton 没有触发它的 Click 事件,原因是我将其 CausesValidation 属性设置为 True。如果您不希望链接验证表单,请务必将其设置为 False。

于 2012-08-16T06:12:01.617 回答
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);
}
于 2010-02-06T10:32:38.010 回答
0

用户控件是否在 gridview 中?如果是这样,请在 gridview 的 onrowcreated 事件上注册事件处理程序。

于 2010-02-06T13:34:49.920 回答
0

看来您有视图状态问题。因为在加载视图状态时控件不存在,所以应用程序不知道如何连接要触发的事件。这是解决此问题的方法。

实际上,您可以通过在 loadviewstateevent 触发后立即加载控件树来使您的应用程序正常工作。如果您覆盖 loadviewstate 事件,调用 mybase.loadviewstate,然后在其后放置您自己的代码以重新生成控件,这些控件的值将在页面加载时可用。在我的一个应用程序中,我使用视图状态字段来保存可用于重新创建这些控件的 ID 或数组信息。

Protected Overrides Sub LoadViewState(ByVal savedState As Object)
    MyBase.LoadViewState(savedState)
    If IsPostBack Then
        CreateMyControls()
    End If
End Sub
于 2010-02-06T19:13:45.467 回答
0

我遇到过同样的问题。我在添加控件的页面上有 viewstate="false"。(在 aspx 页面上)

于 2013-11-19T21:25:51.007 回答