【发布时间】:2015-09-24 22:44:52
【问题描述】:
我有一个简单的 aspx 页面,其中包含 2 个嵌套转发器。每个上面都有按钮。当用户单击外部中继器上的按钮时,我可以在代码隐藏中捕获 ItemCommand,但是当用户单击内部中继器时,我无法捕获它。
我在其他线程中读到需要手动将事件附加到内部中继器,但无法解决。
谁能帮忙?
这里是aspx。我在后面使用 vb.net 代码
<asp:Repeater runat="server" ID="ParentRepeater">
<ItemTemplate>
<li id="P<%#DataBinder.Eval(Container, "DataItem.id")%>">
<%#DataBinder.Eval(Container, "DataItem.name")%>
<asp:Button runat="server" ID="adedit" Text="Edit" CommandName='<%#DataBinder.Eval(Container, "DataItem.id")%>'
class="pages-edit" />
<asp:Button runat="server" ID="addel" Text="Delete" CommandName='<%#DataBinder.Eval(Container, "DataItem.xid")%>'
class="pages-delete" />
<ul class="page-section sub innerdrag">
<asp:Repeater runat="server" ID="childrepeater">
<ItemTemplate>
<li id="<%#DataBinder.Eval(Container, "DataItem.id")%>,">
<%#DataBinder.Eval(Container, "DataItem.name")%><asp:Button runat="server" ID="ad_edit"
Text="Edit" CommandName='<%#DataBinder.Eval(Container, "DataItem.id")%>' class="pages-edit" />
<asp:Button runat="server" ID="ad_del" Text="Delete" CommandName='<%#DataBinder.Eval(Container, "DataItem.xid")%>'
class="pages-delete" />
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</li>
</ItemTemplate>
</asp:Repeater>
仍然没有触发,所以我也将更新后的 VB 放在这里
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim DBFunctions As New DBFunctions.Functions
Dim dstmp As New DataSet
Dim dstmp2 As New DataSet
dstmp = DBFunctions.SQLDataSet("SELECT id,name, 'x'+cast(id as varchar(50)) as xid from pages where parent = 0 and coalesce(active,1)=1 order by orderby asc", "data")
dstmp2 = DBFunctions.SQLDataSet("SELECT id,name , 'x'+cast(id as varchar(50)) as xid,parent from pages where parent >0 and coalesce(active,1)=1 order by orderby asc", "data2")
Dim allData As New DataSet
allData.Tables.Add(dstmp.Tables(0).Copy)
allData.Tables.Add(dstmp2.Tables(0).Copy)
allData.Relations.Add(New DataRelation("Children", allData.Tables(0).Columns("ID"), allData.Tables(1).Columns("parent")))
ParentRepeater.DataSource = allData
ParentRepeater.DataBind()
sdhfunctions.Close()
End If
End Sub
Protected Sub repMenu1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles ParentRepeater.ItemDataBound
Dim dv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
If dv IsNot Nothing Then
Dim repSubMenu As Repeater = TryCast(e.Item.FindControl("childrepeater"), Repeater)
If repSubMenu IsNot Nothing Then
AddHandler repSubMenu.ItemCommand, AddressOf childrepeater_ItemCommand
repSubMenu.DataSource = dv.CreateChildView("Children")
repSubMenu.DataBind()
End If
End If
End Sub
Protected Sub ParentRepeater_ItemCreated(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles ParentRepeater.ItemCreated
Dim dv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
If dv IsNot Nothing Then
Dim repSubMenu As Repeater = TryCast(e.Item.FindControl("childrepeater"), Repeater)
If repSubMenu IsNot Nothing Then
AddHandler repSubMenu.ItemCommand, AddressOf childrepeater_ItemCommand
repSubMenu.DataSource = dv.CreateChildView("Children")
repSubMenu.DataBind()
End If
End If
End Sub
Protected Sub childrepeater_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs)
Dim stophere As String = ""
End Sub
我在 stophere 上放了一个断点,它永远不会被命中。
对不起,我真的很困惑:(
【问题讨论】:
-
这是您使用的真实代码吗?貌似没有订阅外层的Repeater ItemCommand事件
-
itemcommend 位于父中继器的按钮上,我可以在代码隐藏中捕获它们。但是我无法捕获内部中继器按钮上的 itemcommand。
-
你为什么要在后面的代码中捕获这个?
<asp:Repeater runat="server" ID="childrepeater" OnItemCommand="childrepeater_ItemCommand">有什么问题? -
我需要针对单击的按钮捕获唯一引用。因此我把它放在 itemcommand 中
-
非常好,但是你已经有了这个,并且事件处理程序订阅无论如何都不应该有这个信息。老实说,我还是不明白