ASP.NET 用户控件上的链接按钮未触发

发布于 2024-08-20 11:04:30 字数 1386 浏览 5 评论 0原文

我有一个用户控件,它被添加到另一个用户控件中。嵌套用户控件由 GridView、图像按钮和链接按钮组成。根据绑定到 GridView 的结果,嵌套用户控件将作为集合对象添加到外部控件。

我遇到的问题是我的链接按钮不起作用。我点击它,但事件没有触发。连加断点都没有达到。由于嵌套用户控件被添加了多次,我已将图像按钮设置为具有唯一的 id 以及链接按钮。虽然图像按钮与其 JavaScript 一起正常工作。链接按钮需要在后面的代码中触发一个事件,但尽管我付出了一切努力,我还是无法使其工作。我将链接按钮动态添加到控件中。下面是我正在使用的相关代码:

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 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('

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?

,'_'); 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

红焚 2024-08-27 11:04:31

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

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.

如歌彻婉言 2024-08-27 11:04:31

尝试将您的点击事件添加到 linkbutton 标记:

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

或将其添加到您的 Page_Load:

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

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);
}
小兔几 2024-08-27 11:04:31

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

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

笔落惊风雨 2024-08-27 11:04:31

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

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

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

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
扶醉桌前 2024-08-27 11:04:31

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文