We can call a code behind method by using server-side evaluation syntax <%=.
Example:
In code behind, we have:
protected string GetYes() { var yesData = ResponseRepository.GetRepository().GetAllResponses() .Where(r => r.WillAttend.HasValue && r.WillAttend.Value); var html = new StringBuilder(); foreach (var rsvp in yesData) { html.Append($"{rsvp.Name}{rsvp.Email}{rsvp.Phone}"); } return html.ToString(); }
We can get the string value from aspx by following syntax in aspx:
<form id="form1" runat="server"> <h2>RSVP Summary</h2> <h3>People who will attend:</h3> <table> <thead> <tr> <th>Name</th> <th>Email</th> <th>Phone</th> </tr> </thead> <tbody> <strong><%= GetYes() %></strong> </tbody> </table> </form>
Source: ASP.NET 4.5 Pro Apress – Chapter 1