views:

373

answers:

2

How would you create a new html li element dynamically in the code behind (server side)?

How would you access an li in an existing ul element on the server side? I need to FindControl get all li items and then add new li item.

Update

I'm using jquery ajax to access server side, so I must use static WebMethod. FindControl is non/static method.

 <script type="text/javascript">
        $(function(){
        $("#sortable").sortable();
        $("#sortable").disableSelection();
    });

This is jQuery Ajax Call

$(document).ready(function() {
        // Add the page method call as an onclick handler for the div.
        $("#Result").click(function() {
            $.ajax({
                type: "POST",
                url: "DraggableTest.aspx/SomeMethod",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    // Replace the div's content with the page method's return.
                    $("#Result").text(msg.d);
                }
            });
        });
    });

This is CodeBehind

[WebMethod]
    public static string SomeMethod()
    {
        // using System.Web.UI.HtmlControls;
        HtmlGenericControl ul = FindControl("sortable") as HtmlGenericControl;
        if (ul != null)
        {
            foreach (HtmlControl c in ul.Controls)
            {
                if (c.TagName.ToLower() == "li")
                {
                    // Processing here
                }
            }
        }


    }
A: 

You can use HtmlGenericControl to create server side li element. Another alternative might be using LiteralControl.

EDIT : If your <ul> is a server control (has runat="server" attribute) you can access it by Page.FindControl("yourControlID").

// <ul runat="server" id="yourControlID"></ul>

HtmlGenericControl myUl = (HtmlGenericControl)Page.FindControl("yourControlID");
LiteralControl liByLiteral = new LiteralControl("<li>li by LiteralControl</li>");
myUl.Controls.Add(liByLiteral);
HtmlGenericControl newByHtmlGenericControl = new HtmlGenericControl("li");
newByHtmlGenericControl.InnerText = "li by HtmlGenericControl";
myUl.Controls.Add(newByHtmlGenericControl);
// You can access other <li> elements in <ul> control
Canavar
What he said, except we usually utilize Repeaters for the kind of stuff it sounds like you are doing. : - )
rlb.usa
+3  A: 

You can add an Html List Item using the HtmlGenericControl:

System.Web.UI.HtmlControls.HtmlGenericControl li = new System.Web.UI.HtmlControls.HtmlGenericControl("li");
li.InnerHtml = "<b>Some text</b>";
Page.Controls.Add(li);

In the above example, I've just added the control directly to the end of the page - you'll obviously need to consider what parent element you add it to.

Update To answer the extra part of the question that you asked after edit, if your ul control has an id of ulListId and is marked with runat="server, you can find it programmatically using the FindControl method. Then just loop through the ul's children:

        // using System.Web.UI.HtmlControls;
        HtmlGenericControl ul = Page.FindControl("ulListId") as HtmlGenericControl;
        if (ul != null) {
            foreach (HtmlControl c in ul.Controls)
            {
                if (c.TagName.ToLower() == "li")
                {
                    // Processing here
                }
            }
        }
Dexter
Thanks, but what if I use WebMethod call. Method must be static, and FindControl is not static - cannot access snon static method in static context.
nemke
You like adding extra bits! You won't be able to add content directly to your web page from a WebMethod call - you'll need to do this on the client instead.I would recommend that you either:- return an IList<string> or KeyValuePair<string, string> from your webMethod, and handle the presentation of the data as list items on the client (using jQuery), or- if you're really keen to handle the formatting on the server, find the relevant UL control on the screen using jQuery and pass it as a parameter to the webMethod
Dexter
Sorry for adding extra bits :), but I think stack is ideal fore real time solving problem. I added additional explanation. Better to know if I'm using wrong approach. Thanks for your help.
nemke