Add to List from codebehind C# Asp.net

后端 未结 5 1130
春和景丽
春和景丽 2020-12-09 03:10

I have a UL list in a ASPX page:

  • Tab 1
5条回答
  •  萌比男神i
    2020-12-09 03:32

    You can create a dynamic UL by using an asp:Repeater Control

    You can use repeater in following way, in your .aspx file

    
        
        
        
    
    

    And you can put dynamic data via code behind in .aspx.cs file

    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable newsDataTable = new DataTable();
    
        // add some columns to our datatable
        newsDataTable.Columns.Add("href_li");
        newsDataTable.Columns.Add("DisplayText");
    
        for (int i = 1; i <= 5; i++)
        {
            DataRow newsDataRow = newsDataTable.NewRow();
            newsDataRow["href_li"] = "?sc=item_" + i;
            newsDataRow["DisplayText"] = "List Item # "+i;
            newsDataTable.Rows.Add(newsDataRow);
        }
        menu_ul_1.DataSource = newsDataTable;
        menu_ul_1.DataBind();
    }
    

    Result: You will get following html through this code

    
    

提交回复
热议问题