ASP.NET/C# How to access a list item of an unordered list using server runat =?

I have a ul list with a number of list items, for example:

<ul>
<li runat="server" id="liOne">one</li>
<li runat="server" id="liTwo">two</li>
<li runat="server" id="liThree">three</li>
</ul>

      

On page load, I need to set visible or invisible list items.

For some reason, I cannot access them in C # code. Also, no list items are created for the designer file. Anyone have any suggestions on how to get this to work?

I am getting this error:

The name 'liOne' does not exist in the current context

      

Many thanks.

+1


a source to share


2 answers


It should work. Try to repair and restart visual studio.



Then try deleting the designer file and right-clicking on the page and choosing "Convert to Web Application" if it's already a web application.

+2


a source


Your code might be wrapped in some server control that doesn't allow direct links by ID. If so, you can access your internals of that shell by writing this:

var li = wrapperElement.FindControl ("liOne") as HtmlGenericControl;



Hope it helps.

0


a source







All Articles