I have an ASPX page and two user controls. Both of them contain a repeater.
The ASPX page has a user control, and this user control in turn has another user control inside a repeater.
When I click on a row in usrMedlemRow.rptMedlemmar, the showMedlemClick event should be triggered.
However, it doesn’t seem to be triggered, or usrMedlemslista is not catching it.
What did I do wrong?
Code on ASPX page:
<div class="row">
<div class="col-9"><usr:Medlemslista runat="server" ID="usrMedlemsLista" /></div>
<div class="col-3" style="border: 1px solid darkgray; background-color: white; padding: 0px; font-size: 12px; min-height: 700px;">
<usr:MedlemsData runat="server" ID="usrMedlemsdata" Visible="false" />
</div>
</div>
Code on Usercontrol MedlemsLista (usrMedlemsLista):
<asp:Repeater runat="server" ID="rptFastigheter">
<HeaderTemplate />
<ItemTemplate>
<asp:Label runat="server" ID="lblFastighet" Text='<%#DataBinder.Eval(Container.DataItem, "Fastighetsbeteckning")%>' Visible="false" /> <usr:MedlemsRow runat="server" ID="usrMedlemsRow" />
</ItemTemplate>
<FooterTemplate />
</asp:Repeater>
Codebehind:
Public Event showMedlemClick2 As EventHandler
Protected Sub rptFastigheter_ItemDataBound(S As Object, e As RepeaterItemEventArgs) Handles rptFastigheter.ItemDataBound
If e.Item.ItemType = ListItemType.Item Then
Dim control As MedlemRow = DirectCast(e.Item.FindControl("usrMedlemsRow"), MedlemRow)
AddHandler control.showMedlemClick, AddressOf showMedlem2
End If
End Sub
Protected Sub showMedlem2(s As Object, e As CommandEventArgs)
Dim _eventCommands As New CommandEventArgs("MedlemsID", e.CommandArgument)
RaiseEvent showMedlemClick2(Me, _eventCommands)
End Sub
Code on Usercontrol MedlemRow:
<asp:Repeater runat="server" ID="rptMedlemmar">
<HeaderTemplate><div class="row"></HeaderTemplate>
<ItemTemplate>
<div class="col-1"><asp:LinkButton runat="server" ID="lnkNr" Text='<%#DataBinder.Eval(Container.DataItem, "Nr")%>' CommandArgument='<%#DataBinder.Eval(Container.DataItem, "MedlemsID")%>' OnCommand="showMedlem" /></div>
<div class="col-2"><asp:LinkButton runat="server" ID="LinkButton1" Text='<%#DataBinder.Eval(Container.DataItem, "Fastighetsbeteckning")%>' CommandArgument='<%#DataBinder.Eval(Container.DataItem, "MedlemsID")%>' OnCommand="showMedlem" /></div>
<div class="col-3"><asp:LinkButton runat="server" ID="LinkButton2" Text='<%#DataBinder.Eval(Container.DataItem, "Namn")%>' CommandArgument='<%#DataBinder.Eval(Container.DataItem, "MedlemsID")%>' OnCommand="showMedlem" /></div>
<div class="col-2"><asp:LinkButton runat="server" ID="LinkButton3" Text='<%#fixMobil(DataBinder.Eval(Container.DataItem, "Mobil"))%>' CommandArgument='<%#DataBinder.Eval(Container.DataItem, "MedlemsID")%>' OnCommand="showMedlem" /></div>
<div class="col-3"><asp:LinkButton runat="server" ID="LinkButton4" Text='<%#DataBinder.Eval(Container.DataItem, "Epost")%>' CommandArgument='<%#DataBinder.Eval(Container.DataItem, "MedlemsID")%>' OnCommand="showMedlem" /></div>
<div class="col-1"> </div>
</ItemTemplate>
<FooterTemplate></div></FooterTemplate>
</asp:Repeater>
Codebehind:
Public Event showMedlemClick As EventHandler
Protected Sub showMedlem(s As Object, e As CommandEventArgs)
Dim _eventCommands As New CommandEventArgs("MedlemsID", e.CommandArgument)
RaiseEvent showMedlemClick(Me, _eventCommands)
End Sub
Please help me, I’m really stuck.
2
Answers
I have found the problem.
In the repeater
rptFastighet
on the pageusrMedlemslista
, I wasn't usingalternateItem
.That's why I didn't have it in my code-behind on the line
If e.item.itemType = ListItemType.Item
.It turns out the repeater has it in the background anyway, so I added
OrElse e.item.ItemType = ListItemType.AlternateItem
, and then it worked.The thing is, when I was testing, I kept clicking on the same row every time, and it didn't work. This morning, I tried multiple different rows, and then every other row worked.
Thanks, Lajos Arpad, you helped me rethink the problem and move forward.
Try adding
to your divs. Also, you might consider changing
OnCommand
toOnServerClick
, but that’s up to you to decide. See more here.