If Else statement in webforms aspx page
In an ASP.NET Web Forms application, you can use server-side code to implement conditional logic using if
and else
statements directly within your ASPX page. Here's a basic example of how you can do this:
<%@ Page Language="C#" %>
<!DOCTYPE html>
<html>
<head>
<title>Conditional Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<% if (DateTime.Now.Hour < 12) { %>
<h1>Good morning!</h1>
<% } else { %>
<h1>Good afternoon!</h1>
<% } %>
</div>
</form>
</body>
</html>
In this example:
<% %>
are inline code blocks in ASP.NET Web Forms. They allow you to execute server-side code within the ASPX page.
- Inside the
<% %>
code blocks, you can write C# (or VB.NET) code directly.
- The
DateTime.Now.Hour
property is used to get the current hour of the day.
- Based on the value of the current hour, either "Good morning!" or "Good afternoon!" is displayed using an
if
-else
statement.
When the page is rendered, the server-side code will execute, and the appropriate message will be displayed based on the current time.