HTMLAnchor inside a repeater

G

Guest

I have an HTMLAnchor control on my aspx page. When it's not inside a
repeater, it works fine. When I put it inside a repeater control, the handler
never gets fired. I have a handler for the htmlAnchor's ServerClick event
(works when not inside a repeater), and a handler for the repeater's
ItemCommand event. My html and vb source code is shown below, but what I
think I need is a simple working example in vb.net. Can anyone help?
======================================
<a ID="haAnchor" OnServerClick="haAnchor_Click" runat="server">Click here</a>
======================================
In my code behind:
======================================
....
Protected WithEvents haAnchor As System.Web.UI.HtmlControls.HtmlAnchor
....

Public Sub haAnchor_Click(ByVal sender As Object, ByVal e As EventArgs)
Handles haAnchor.ServerClick
Me.Label1.Text = "HTML Anchor ServerClick fired!"
End Sub
======================================

When I put my HTMLAnchor control inside a repeater, the ServerClick event
does not fire:
=========================================
<table>
<asp:Repeater id=rpCustomers OnItemCommand="rpCustomers_ItemCommand"
EnableViewState="False" runat="server" DataSource="<%# dvwCustomerDisplays
%>">
<ItemTemplate>
<tr>
<td>
<a ID="haAnchor" OnServerClick="haAnchor_Click" runat="server">Click
here</a>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
=====================================
In my code behind, I have the same handler as before, plus this one:
=====================================
Public Sub rpCustomers_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles
rpCustomers.ItemCommand
Me.Label1.Text = "HTML Anchor ItemCommand fired!"
End Sub
======================================
 
K

Ken Cox [Microsoft MVP]

Hi Leigh,

I'm not clear on what you're aiming to accomplish, but here's what I managed
to do with your code:


<table border="1">
<asp:repeater id="rpCustomers"
OnItemCommand="rpCustomers_ItemCommand" EnableViewState="False"
runat="server">
<itemtemplate>
<tr>
<td>
<a id="haAnchor"
onserverclick="haAnchor_Click" runat="server">Click here</a>
</td>
</tr>
</itemtemplate>
</asp:repeater>
</table>
</form>
<p>&nbsp;</p>
<p>
<asp:label id="Label1" runat="server">Label</asp:label></p>

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dvwCustomerDisplays As DataView
dvwCustomerDisplays = CreateDataSource().DefaultView
rpCustomers.DataSource = dvwCustomerDisplays
rpCustomers.DataBind()
End Sub

Public Sub haAnchor_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim htmlAnchor As HtmlAnchor
htmlAnchor = sender
Label1.Text = "HTML Anchor ServerClick fired!" & _
htmlAnchor.UniqueID
End Sub

Public Sub rpCustomers_ItemCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) _
Handles rpCustomers.ItemCommand
Response.Write(e.Item.ItemIndex.ToString)
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
 
G

Guest

Thanks, Ken. The problem was that I had omitted the quotation marks around
the repeater's ID attribute. IOW, I had:

<asp:repeater id=rpCustomers ...

when I should have had:

<asp:repeater id="rpCustomers" ...

Sheesh -- can I please have the four hours back I spent chasing my tail on
this one? What a bonehead. :-(

Hmmm -- too bad the syntax checker doesn't pick this one up.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top