开发者

Adding html list items dynamically from code behind?

开发者 https://www.devze.com 2023-04-06 07:36 出处:网络
The above is the code in my .aspx page. How this can be added from code behind dyanmically? <ul runat=\"server\" id=\"1\">

The above is the code in my .aspx page. How this can be added from code behind dyanmically?

<ul runat="server" id="1">
<li><a href="page.开发者_如何转开发html">abc</a>
  <ul runat="server" id="2">
  <li><a href="pag2.htm">3</a></li>
  <li><a href="page3.htm">2</a></li>
  </ul>
</li>
</ul>


you can take the analogy of the tutorial given in this link : http://neimke.blogspot.com/2011/01/create-delicious-user-interface-for.html it worked for me - It dynamically adds the list items using the given code below using jquery .. check it pout ...

 <li id="tagInputListItem"><input class="tagInput" id="tagInput" /></li>


You can put a PlaceHolder in your .aspx and give it an id, then use that id in code behind page and add controls to that placeholder. For more information you can see in here.

And if you're really sure about "runat=server" attribute maybe this post of mine it's useful (here)

If you need clarifications give me a feedback.


You must use the "InnerHtml" property of "sidebarmenu1" control.

protected void Page_Load(object sender, EventArgs e)
        {
            this.loadHtml();
        }

So you can generate every list item code and add it to the InnerHtml:

private loadHtml()
{
    this.sidebarmenu1.InnerHtml = GetListHtml().ToHtmlString();
}

And a little example for this GetListHtml:

public string GetListHtml()
        {
            StringBuilder htmlBuilder = new StringBuilder();

            htmlBuilder.AppendLine("<li><a href="#">Flat</a>");
            htmlBuilder.Append("<ul runat="server" id="sidebarmenu2">");
            htmlBuilder.AppendLine("<li><a href="#">Flat 1`enter code here`</a></li>");
            htmlBuilder.Append("<li><a href="#">Flat 2</a></li></ul>");
            return htmlBuilder.ToString();
        }

This GetListHtml method can call to a DAL or load data from any other place... use a foreach to load every item...


You can use ASP Literal to populate data from back-end code eg. if you have literal with id ltrNavigation

protected void Page_Load(object sender, EventArgs e)
{
       ltrNavigation.text = "";
        if (!IsPostBack)
        {
           ltrNavigation.text += "<ul id='sidebarmenu1'>";
           ltrNavigation.text += "<li><a href='#'>Flat</a></li>";
           ltrNavigation.text += "</ul>";
        }

}
0

精彩评论

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