0

I'm trying to check if a list item (LI) exists in an unfiltered list (UL). I want to check this by the list item ID.

I am doing this to check if a drag and drop item still exists in the list

i.e. Does LI1 exist in Sortable1

<ul id="sortable1" runat="server"  class="connectedSortable">
  <li id="LI1" runat="server" class="ui-state-default"></li>
  <li id="LI2" runat="server" class="ui-state-default"></li> 
  <li id="LI3" runat="server" class="ui-state-default"></li>
  <li id="LI4" runat="server" class="ui-state-default"></li>
  <li id="LI5" runat="server" class="ui-state-default"></li>
</ul>

I tried the below code, which didn't work at all:

foreach (Control item in sortable1.Controls)
            {
                if (item.Controls.Contains(LI1))
                {
                    lbTest.Text = "LI1 is in sortable 1";
                }
                else
                {
                    lbTest.Text = "LI1 is not in sortable 1";
                }
            }

Thanks for your help

Gareth
  • 3
  • 3
  • `I'm trying to check if a list item (LI) exists in an unfiltered list (UL).`, can you please post this code you've tried so we can further help you? – Trevor Nov 01 '21 at 14:42
  • Please [see](https://stackoverflow.com/questions/28327229/asp-net-find-control-by-id/48386543) that post, it will be helpful. – Trevor Nov 01 '21 at 14:58

1 Answers1

0
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (Control MyControl in sortable1.Controls)
        {
            Debug.Print(MyControl.ID);
            if (MyControl.ID == "LI1")
            {
                Debug.Print("Found LI1");
            }
        }

        // OR
        Control MyControl2 = sortable1.FindControl("LI1");
        if (MyControl2 != null)
            Debug.Print("found LI1");

        // Or this

        if (sortable1.FindControl("LI1") != null)
        {
            // FOUND it
            Debug.Print("found li1");

        }
    }

As above shows, you can use a foreach loop, but you can also as above shows also use a findcontrol to get the control you want, or as the last example shows, you can just test/check the findcontrol expression.

On the other hand? Since you have runat server tags, then you can in code directly use the LI1 control like this:

debug.print (LI1.ID);
Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
  • Thank you so much for your help Albert! This is really appreciated. I'll try this solution in the morning when I'm back in work. Thanks again! – Gareth Nov 01 '21 at 17:28