开发者

How should the IParserAccessor interface be used in custom ASP.NET controls

I am trying to write a template custom control in C#, ASP.NET that will have a collection of items. I'd like to use it similarly to the built-in DropDownList control in ASP.NET, but my items will have different properties.

I noticed that the ListItem class that is used to represent the items in a DropDownList control (or any list control) implements the IParserAccessor interface. When should I implement this interface and what should I do in its AddParsedSubObject(object obj). I've searched the internet but I could not find an example or 开发者_运维技巧a good explanation for it.

I assume that my custom control's items will make it without implementing that interface, but still, just out of curiosity, I'd be glad to know more about it.


You don't need to implement IParserAccessor to get functionality like what you are talking about. Instead create a collection to hold the child controls (we'll call it Items) in your server control and apply the ParseChildren attribute to your server control with "Items" as the DefaultProperty value.

The following link contains an example on how to implement ParseChildren:
http://msdn.microsoft.com/en-us/library/aa310907(v=vs.71).aspx

Update
Information on IParserAccessor :

From: http://msdn.microsoft.com/en-us/library/system.web.ui.iparseraccessor.aspx

Because the Control class implements this interface, it is easier to extend that class and override its implementation of the AddParsedSubObject method than to implement this interface yourself.

From: http://forums.asp.net/t/879797.aspx/1 (see imagemaker's post):

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.

Here is an examle from the same post:

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 If
End 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 If
End Sub

Here, after checking that obj is a StyledItem, it is added to the Items collection of the control.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜