Last post Apr 24, 2005 08:06 PM by imagemaker
Apr 24, 2005 02:14 PM|SomeNewTricks2|LINK
Could anyone please tell me why do some server controls implement the method
AddParsedSubObject ? When creating new controls, do we need that method why ?
Apr 24, 2005 07:54 PM|Caddre|LINK
In Asp.net 1.1 you only need the IPostbackDataHandler interface. Try these urls for more info and pick up Programming Microsoft .NET by Jeff Prosise if you write C#. There is an easy way use the place holder control and you can get all the events generated
for you. Hope this helps.
Apr 24, 2005 08:06 PM|imagemaker|LINK
AddParsedSubObject is the only method of the IParserAccessor interface. The IParserAccessor interface is implemented by Control and all classes inheriting from Control as well as the ListItem class. When child controls or elements of server controls that
implement the IParserAccessor interface are parsed from the HTML, the AddParsedSubObject method is called with the following method signature follows:
VB -- Sub AddParsedSubObject(ByVal obj As Object) C# -- void AddParsedSubObject(object obj)
The obj parameter represents the Object that has been parsed. In your custom server controls overridden AddParsedSubObject method, you can then check the type of obj then cast and process it accordingly, for example as a literal control or as a specific
child element, by adding it to a child controls collection, item collection or whatever.
Where I first ran into dealing with implementing the IParserAccessor interface and the AddParsedSubObject was in defining the class for a child element that needed to contain as its inner content a string:
<WESNet:StyledList id="StyledList1" runat="server"> <WESNet:StyledItem Value="2" ForeColor="red">Two</WESNet:StyledItem> <WESNet:StyledItem Value="5" ForeColor="green">Five</WESNet:StyledItem></WESNet:StyledList>
Because my StyledItem class did not inherit from Control, I had to implement IParserAccessor and its AddParsedSubObject as follows:
Protected Sub AddParsedSubObject(ByVal obj As Object) Implements IParserAccessor.AddParsedSubObject If TypeOf obj Is LiteralControl Then Me.Text = CType(obj, LiteralControl).Text Else Throw New ArgumentException("Inner content of StyledItem must contain only static text") End IfEnd Sub
Then, in my StyledList class (which inherited from WebControl), I needed to override its AddParsedSubObject method as follows:
Protected Overrides Sub AddParsedSubObject(ByVal obj As Object) If TypeOf obj Is StyledItem Then Dim si As StyledItem = CType(obj, StyledItem) Items.Add(si) Else Throw New ArgumentException ("A StyledList server control may contain only StyledItems") End IfEnd Sub
Here, after checking that obj is a StyledItem, it is added to the Items collection of the control.
Hope this explanation helps!