can code inside a Repeater's ItemTemplate modify controls in the ItemTemplate?

B

Bennett Haselton

If I have this code for repeater, it gives a compilation error saying
"The type or namespace name 'MyValue2' could not be found":

<asp:Repeater id="MyList" runat="server">
<ItemTemplate>
<%#Container.DataItem%><br>
<% MyValue2.Text = "ghi"; %>
<asp:TextBox ID="MyValue2" Runat="server"/>
<br>
</ItemTemplate>
</asp:Repeater>

Presumably that's because the MyValue2 control is inside an
ItemTemplate that gets repeated multiple times, so each instance of
MyValue2 gets a unique ID when the page is actually rendered, and the
C# code can't find the "MyValue2" because they were given a series of
unique names instead. (In fact I can even see their IDs in the source
code of the rendered page -- MyList__ctl0_MyValue2,
MyList__ctl1_MyValue2, etc.)

So, is there a way to have inline code inside the ItemTemplate that
can modify a control that's also repeated inside the template?

I have four ASP.Net books and I can't find any examples in any of them
showing how to do this... Maybe it can't be done?

-Bennett
 
J

Joe Fallon

I use the ItemDatBound in my grids to manipulate stuff.
I assume you can do the same for a Repeater.
===========================================
Sample code:
===========================================
Private Sub dg1_ItemDataBound(ByVal sender As System.Object, ByVal e As
DataGridItemEventArgs) Handles dg1.ItemDataBound
If e.Item.ItemType = ListItemType.Header Then
'do something to the header
End If

If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
ListItemType.AlternatingItem Then
'need to take paging into account!
Dim mIndex As Integer = CType(sender, DataGrid).PageSize *
CType(sender, DataGrid).CurrentPageIndex + e.Item.ItemIndex

'set Status hyperlink
Dim oHylStatus As HyperLink = CType(e.Item.FindControl("hylStatus"),
HyperLink)
If mCollection(mIndex).statustext = "None" Then
oHylStatus.NavigateUrl = ""
Else
oHylStatus.NavigateUrl = "javascript:..;"
End If
oHylStatus.Text = mCollection(mIndex).statustext

If mCanAmend=True Then
Dim oBtnAmend As ImageButton = CType(e.Item.FindControl("btnAmend"),
ImageButton)
If mCollection(mIndex).status = "XYZ" Then
oBtnAmend.Visible = True
Else
oBtnAmend.Visible = False
End If
End If

End If
End Sub
 

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

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top