Control names within a repeater

R

Ryan Ternier

Hello,

I have a repeater control that does the following on the ItemDataBound
event. It works perfectly, except that when it prints out, the names of the
controls
aren't what I'd expect.

lstYes.ID = "lstY" & drvTemp("UserID") 'Assuming ID is the part we want form
the DB

I use that to set the ID of the listBox, but when it comes to the screen it
has a name of

rptSecurity:ctrl00:lstYes14

Or something similar to that.

Is there a way to make that control just have lstYes14?


If Not e.Item.DataItem Is Nothing Then
Try
Dim intTypeID = CInt(Request.QueryString.Get("TypeID"))
Dim objItemType As ItemType
objItemType = New ItemType(CInt(intTypeID))

'Get the current item that's being bound from the DB
Dim drvTemp As DataRowView = CType(e.Item.DataItem, DataRowView)
'Get handles on the server side controls so we can edit them through
code. l337 pwn4g3
Dim lstYes As ListBox = CType(e.Item.FindControl("listYes"),
ListBox)
Dim lstNo As ListBox = CType(e.Item.FindControl("listNo"), ListBox)
Dim lblButtons As Label = CType(e.Item.FindControl("LabelButtons"),
Label)
Dim lblName As Label = CType(e.Item.FindControl("LabelName"), Label)


lstYes.ID = "lstY" & drvTemp("UserID") 'Assuming ID is the part we
want form the DB
lstYes.SelectionMode = ListSelectionMode.Multiple
lstNo.ID = "lstN" & drvTemp("UserID") 'Assuming ID is the part we
want form the DB
lstNo.SelectionMode = ListSelectionMode.Multiple
lblName.Text = drvTemp("FullName")
'Response.Write(drvTemp("FullName") & "<BR>")
For i As Integer = 0 To objItemType.Layout.Count() - 1
lstYes.Items.Add(objItemType.Layout.Item(i).Field.Caption())
lstYes.Items(i).Value = objItemType.Layout.Item(i).Field.FieldID
Next

'Put raw HTML into the Label control to build the 4 button table to move
items between the list boxes.
'To switch the items between listboxes
'onclick="SwitchList(document.getElementById('lstNotifyAvailable'),
document.getElementById('lstNotifySelected'));AdNotify(document.getElementBy
Id('lstNotifyAvailable'));
With lblButtons
Text = "<table>"
.Text &= "<tr><td>"
.Text &= "<input type=""button"" class=""BoxType42""
value=""Allow All"" name=""btn" & drvTemp("UserID") & "AA""
onclick=""SwitchList(document.getElementById('" & lstYes.ID.ToString & "'),
document.getElementById ('" & lstNo.ID.ToString &
"'));AddNotify(document.getElementById('" & lstYes.ID.ToString() & "'));"">"
.Text &= "</td></tr>"
.Text &= "<tr><td>"
.Text &= "<input type=""button"" class=""BoxType42""
value=""Allow"" name=""btn" & drvTemp("UserID") & "A""
onclick=""Allow(document.Security." & lstYes.ID.ToString() &
",document.Security." & lstNo.ID.ToString() & ");"">"
.Text &= "</td></tr>"
.Text &= "<tr><td>"
.Text &= "<input type=""button"" class=""BoxType42""
value=""Disallow All"" name=""btn" & drvTemp("UserID") & "DA""
onclick=""DisAll(document.Security." & lstYes.ID.ToString() &
",document.Security." &
lstNo.ID.ToString() & ");"">"
.Text &= "</td></tr>"
.Text &= "<tr><td>"
.Text &= "<input type=""button"" class=""BoxType42""
value=""Disallow"" name=""btn" & drvTemp("UserID") & "D""
onclick=""Disallow(document.Security." & lstYes.ID.ToString() &
",document.Security." &
lstNo.ID.ToString() & ");"">"
.Text &= "</td></tr>"
.Text &= "</table>"
End With

Catch ex As Exception
Debug.WriteLine(ex.ToString)
Response.Write("<BR>_______________________<BR>" & ex.ToString &
"<BR>")
Response.Flush()
End Try

End If
 
T

Teemu Keiski

Hi,

no there isn't. ASP.NET gives controls the IDs based on their location in
control hierarchy and if control is contained in a naming container, which
RepeaterItem is, it is named in new scope (scope of the repeaterItem where
it is contained) and named according to that. This way it is ensured that
every control has unique ID on the Page.

What's the issue with this? You can get reference to the control via the
RepeaterItem (callint RepeaterItem's FindControl method) and there you could
use plain ID (lstYes14). If you need the ID at client-side, you can get it
with controls ClientID property which returns the unique ID as
clientscript-friendly. Also you can locate the control from top-level
control (outside the Repeater) by using Page.FindControl and assigning the
complete unique ID string, though seems that you are not looking for that.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke
 
R

Ryan Ternier

The issue i'm having is my repeater will be repeating multiple sets of 2
list boxes.

I'm using client side JS to move items from 1 listbox to the other, for each
repeating.

I didn't know you could grab the ClientID property... i'll have to look into
that, and see if I can get that to work.

Thanks!
 
S

Shan Plourde

I've had to do this a few times. Here's a code snippet:
<a
href="javascript:eek:penWin('<%= tbCompanyName.UniqueID.Replace(":",
"_") %>', '<%= tbCompanyId.UniqueID.Replace(":", "_") %>')">Find</a>

This code snippet calls a javascript function. Control.UniqueID gives
you the full name of a control
Shan Plourde
 
T

Teemu Keiski

You don't need to do those replaces at all if you'd use ClientID property
instead of UniqueID...
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top