asp.net · ebook

[ASP.NET] Calling a code-behind method

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

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s