Dynamically create HTML in ASP.NET

I was interested to know if asp.net allows dynamically generating an HTML string on an .aspx source page (rather than code).

For testing, I created the following simple .aspx page ...

In my asp.net code, I have the following:

    protected List<string> myList = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (myList == null)
            myList = new List<string>();

        myList.Add("One String");
        myList.Add("Two String");
        myList.Add("Three String");
        myList.Add("Four String");

        this.Repeater1.DataSource = myList;
        this.Repeater1.DataBind();
    }

On the corresponding source page, I:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <ol>
        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                <li>
                    <%# DataBinder.GetDataItem(myList) %>
                </li>
            </ItemTemplate>
        </asp:Repeater>
    </ol>
</body>
</html>

The resulting .aspx page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title></head>
<body>
    <ol>

                <li></li>

                <li></li>

                <li></li>

                <li></li>

    </ol>
</body>
</html>

Notice that the Repeater element actually created four list items. However, the contents (One String, Two String, etc.) of the myList was not sent for the trip.

What do I need to do to evaluate the list myList and get its values โ€‹โ€‹inside the tags of the list item? By the way, I do not deal with how to use the repeater specifically, so if there is a solution to this problem that does not include the Repeater control, I am fine with that.

. , "myList" asp: BulletedList . HTML- .

+3
1

:

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
         <li>
            <%# Container.DataItem %>
         </li>
    </ItemTemplate>
</asp:Repeater>

, :

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
         <li>
            <%# Eval("PropertyName") %>
            or
            <%# Eval("PropertyName","DataFormat") %>
         </li>
    </ItemTemplate>
</asp:Repeater>

, . !!!

, .

+6

Source: https://habr.com/ru/post/1779689/


All Articles