How is the server-side value being assigned in this ASP.NET custom control?
I am looking at somebody else's code and in it there is a user control:
<UserControl:Comments ID="Comments1" runat="server" ObjectID="4" ObjectRecordID='<%#((Alert)Container.DataItem).AlertI开发者_JAVA百科d %>'></UserControl:Comments>
What I don't quite understand is how the value for ObjectRecordID gets assigned. I understand by looking at the code that AlerId is getting assigned to ObjectRecordID but how is ((Alert)Container.DataItem).AlertId grabbing its value?
Thanks.
Container
refers to the parent control of the comment box, which would be a single "row" of the Repeater.
Container.DataItem
is a single object from the collection to which the Repeater is databound. Specifically, the object that is bound to the "row" being processed.
(Alert)Container.DataItem
takes the DataItem and casts it as an Alert
reference.
.AlertId
grabs the AlertId
property of the Alert
object that resulted from the previous step, so that its value can be assigned to the ObjectRecordID
property of the Comments
control.
the <%# %>
element signals to ASP.NET that you are databinding the expression; Because of that, and the fact that it is contained within the ItemTemplate of a Data-bindable control, the property Container.DataItem
is now available. Container.DataItem
is an Object
(which is why it is being cast to the Alert
type) representing the current item in the list of data objects being bound to the control.
精彩评论