Asp.net method call from interface

I have a method at my end that I would like to call from my interface but cannot make it work. here is my code:

<% foreach(string item in Plants){ %> <li> <span class="folder"> <asp:label ID="lblPlantName" runat="server" Text='<% GetPlantName(item) %>'></asp:label> </span> </li> <%} %> 

The getplantName method should return a string and fill the text. But for some reason this is not caused.

Anyone have any ideas or suggestions?

+4
source share
3 answers

Use <%= GetPlantName(item) %> instead of <% GetPlantName(item) %> , and the method must be public or secure.

+9
source

To return a string, you will need Response.Write , which is abbreviated as <%=%> like this:

 <%= GetPlantName(item) %> 
+8
source

While your code can work (with a fix suggested by others), this is not a good practice. This is the classic way of ASP when you use ASP.NET - it is like driving 10 MPH with a sports car on the highway.

One good practice might be to use Repeater Control - it is still simple and much more elegant.

Now .aspx will look like this:

 <asp:Repeater ID="rptPlants" runat="server"> <HeaderTemplate><ol></HeaderTemplate> <FooterTemplate></ol></FooterTemplate> <ItemTemplate> <li> <span class="folder"> <%# Container.DataItem %> </span> </li> </ItemTemplate> </asp:Repeater> 

And for data binding there is such code in the Page_Load function in your code behind:

 string[] arrPlants = new string[] { "Sacred Datura", "Kambroo", "Wallflower", "Beech 'Retroflexa'", "Zephyr Flower" }; rptPlants.DataSource = arrPlants; rptPlants.DataBind(); 

In your case, just replace arrPlants with your real array, Plants .

Feel free to ask for details or explanations. :)

+3
source

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


All Articles