Referencing a specific repeater item instance
-
03-07-2021 - |
Pergunta
I've been looking all over StackOverflow and Google for an answer to this question, but I haven't been able to find anything.
I have a C# based ASP webpage that uses a repeater control to list information about students returned via a SQL procedure. To populate the repeater, the web app I am working in uses a Student
class with [DataMember]
properties representing each field in a SQL query result row. From my understanding, each item in the repeater control contains a separate instance of the Student
class, with each instance representing a row in the query results. First, please let me know if the repeater operates differently than I understand.
If my understanding is correct, however, I would like to know if there is any way to reference a student
instance contained in the specific item of the repeater control. I looked at a few methods associated with the repeater object, including:
repeater.Items //This gets a collection of objects
repeater.DataMember //Gets the 'specific table to bind'
repeater.DataSource //References the SQL query results
None of these seem to reference the SQL row/'Student' instance.
Is there something that I am missing or not understanding correctly? I have been pouring over the MSDN site for the repeater control as well, but I may not be understanding all the terminology, if there is a solution on the page. The MSDN repeater page I am using is http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater(v=vs.100).aspx.
Solução
The items are stored in the repeater.Items statement and you can cast the items to whatever type you are expecting:
var student = (Student)repeater.Items[3];