ASP.NET 按钮单击未捕获(用户控件中的按钮在 Repeater 中动态加载)
我编写了一个用户控件,它捕获一些用户输入,并有一个“保存”按钮将其保存到数据库。我使用中继器在页面上呈现许多此类控件 - 想象一个多项选择问题的列表,每个问题旁边都有一个“保存”按钮。
我正在转发器的 ItemDataBound 事件中加载用户控件,如下所示(代码简化):
Protected Sub rptAssignments_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptAssignments.ItemDataBound
Dim CurrentAssignment As Assignment = DirectCast(e.Item.DataItem, Assignment)
Dim ctl As UA = CType(LoadControl("~\Controls\UA.ascx"), UA)
ctl.AssignmentID = CurrentAssignment.AssignmentID
ctl.Assignment = CurrentAssignment.AssignmentName
ctl.EnableViewState = True
e.Item.Controls.Add(ctl)
End Sub
仅供参考,我需要在运行时加载控件,而不是在 ItemTemplate 中指定它,因为每行可以使用不同的控件。
在用户控件中,有一个像这样的链接按钮:
<asp:LinkButton ID="lbnUpdate" runat="server" Text="Update" OnClick="lbnUpdate_Click" />
... 和一个像这样的按钮单击处理程序:
Protected Sub lbnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lbnUpdate.Click
' my code to update the DB
End Sub
问题是,当单击“保存”按钮时,页面会回发,但不会调用 lbnUpdate_Click。然而,页面本身的 Page_Load 事件被调用。
我应该提到的是,转发器是用户控件的一部分,并且该用户控件被加载到另一个用户控件中(这是一个大量使用用户控件的 DotNetNuke 站点)。保存按钮链接如下所示:
javascript:__doPostBack('dnn$ctr498$AssignmentsList$rptAssignments$ctl04$ctl00$lbnUpdate','')
I have written a user control that captures some user input and has a Save button to save it to the DB. I use a repeater to render a number of these controls on the page - imagine a list of multiple choice questions with a Save button by each question.
I am loading the user control inside the repeater's ItemDataBound event like this (code simplified):
Protected Sub rptAssignments_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptAssignments.ItemDataBound
Dim CurrentAssignment As Assignment = DirectCast(e.Item.DataItem, Assignment)
Dim ctl As UA = CType(LoadControl("~\Controls\UA.ascx"), UA)
ctl.AssignmentID = CurrentAssignment.AssignmentID
ctl.Assignment = CurrentAssignment.AssignmentName
ctl.EnableViewState = True
e.Item.Controls.Add(ctl)
End Sub
FYI, I need to load the control at runtime rather than specify it in the ItemTemplate because a different control could be used for each row.
In the user control, there is a linkbutton like this:
<asp:LinkButton ID="lbnUpdate" runat="server" Text="Update" OnClick="lbnUpdate_Click" />
... and a button click handler like this:
Protected Sub lbnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lbnUpdate.Click
' my code to update the DB
End Sub
The problem is that when the Save button is clicked, the page posts back, but lbnUpdate_Click is not called. The Page_Load event of the page itself is called however.
I should mention that the repeater is part of a user control, and that user control is loaded inside another user control (this is a DotNetNuke site which makes heavy use of user controls). The Save button link looks like this:
javascript:__doPostBack('dnn$ctr498$AssignmentsList$rptAssignments$ctl04$ctl00$lbnUpdate','')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个问题体现了 Webforms 如何战胜自己。
您必须通过重新绑定或从视图状态来重构中继器,以使子控件引发事件。您付出的代价要么是再次访问数据源,要么是在视图状态中存储在客户端上的所有冗余数据。可耻!
This problem exemplifies how webforms outsmarts itself.
You have to reconstitute the Repeater, either by re-binding or from viewstate, to have sub-controls raise events. The price you pay is either another trip to your data source or all that redundant data stored on the client in the viewstate. Shameful!
我曾经遇到过类似的问题,可能是同一件事。
简而言之,由于您是动态创建按钮,因此在回发后它们将不存在。因此,当 ASP.NET Webforms 查找该事件时,它找不到任何内容。
您的中继器何时进行数据绑定?尝试在回发中再次将按钮渲染到页面(即使作为测试),看看是否有效。
I had a similar problem once that might be the same thing.
In short, since you are dynamically creating the buttons, after the postback they don't exist. Thus, when ASP.NET Webforms looks for the event, it can't find anything.
When does your repeater get databound? Try rendering the buttons to the page again in the postback (even as a test) to see if that does the trick.
每次回发时 UserControl 的 ID 都相同吗?
Are the UserControl's IDs same on every postback?