populate a html list control using .NET

前端 未结 4 692
暗喜
暗喜 2021-02-06 05:13

I have a list defined like so:

  • Item 1
4条回答
  •  抹茶落季
    2021-02-06 05:51

    I'm assuming that there is a valid reason for you not to use the BulletedList webserver control. Anyway, this is an interesting programming exercise that illustrates the internals of the Htmlservercontrol architecture and how they map to simple HTML tags.

    The HTML ul and li tags are not directly mapped as HTMLServercontrols. This means that even if you add a runat="server" attribute to the list, it's contents will not be directly accessible as listitems.

    However, all controls not directly mapped as Html server controls are accessible via the HtmlGenericControl class. This makes it possible to create and modify such controls dynamically.

    The solution, therefore, is twofold:

    • Make the unordered list runat="server" so that you can access it in server-side code. Also, you should make the existing items in the list runat="server", else they will be only be accessible as an LiteralControl that contains the first two listitems as plain text.
    • In code, access the contents of the list and add a new HtmlGenericControl of type "li" to it.

    The following (bare-bones simple) page demonstrates this procedure :


    <%@ Page Language="VB" AutoEventWireup="false" %>
    <%@ Import Namespace="System.Collections.Generic" %>
    
    
    
    
    
      Test Page
    
    
      
    • Item 1
    • Item 2

提交回复
热议问题