If statement in aspx page

asp.netIf Statement

asp.net Problem Overview


I want to write a basic if statement on my site to display either item 1 or item 2 depending on if a variable is set to true.

I'm not too familiar with .NET and need a little help with the basic structure of how to get an if statement to work on the aspx page

asp.net Solutions


Solution 1 - asp.net

if the purpose is to show or hide a part of the page then you can do the following things

  1. wrap it in markup with

    <% if(somecondition) { %> some html <% } %>

  2. Wrap the parts in a Panel control and in codebehind use the if statement to set the Visible property of the Panel.

Solution 2 - asp.net

Just use simple code

<%
if(condition)
{%>

html code

<% } 
else 
{
%>
html code
<% } %>

Solution 3 - asp.net

Normally you'd just stick the code in Page_Load in your .aspx page's code-behind.

if (someVar) {
    Item1.Visible = true;
    Item2.Visible = false;
} else {
    Item1.Visible = false;
    Item2.Visible = true;
}

This assumes you've got Item1 and Item2 laid out on the page already.

Solution 4 - asp.net

A complete answer for optional content in the header of a VB.NET aspx page using a master page:

 <%@ Page Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="some_vb_page.aspx.vb" Inherits="some_vb_page" %> 
 <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">          
     <% If Request.QueryString("id_query_param") = 123 Then 'Add some VB comment here, 
         'which will not be visible in the rendered source code of the aspx page later %>        
         <!-- add some html content depending on -->
         <!-- the condition in the if statement: -->                
         <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js" type="text/javascript" charset="utf-8"></script>
     <% End If %>
</asp:Content>

Where your current page url is something like:

http://mywebpage.com/some_vb_page.aspx?id_query_param=123

Solution 5 - asp.net

To use C# (C# Script was initialized at 2015) on ASPX page you can make use the following syntax.

Start Tag:- <% End tag:- %> Please make sure that all the C# code must reside inside this <%%> .

Syntax Example:-

  • `<%@ Import Namespace="System.Web.UI.WebControls" %>`  (For importing Namespace)
    

Reference to some basic namespaces for working with ASPX page.

`<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Diagnostics" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.IO" %>`

C# Code:-

`<%
if (Session["New"] != null)
{
    Page.Title = ActionController.GetName(Session["New"].ToString());
}
%>`

Features of C# Script:

  • No need of compilation. Run time execution is occurred like Java Script.

Before using C# script make sure the following things:-

  • You are on WebForm. Not on WebForm with master page.

  • If you are in WebForm with master page make sure that you have written your C# script at Master page file.

  • C# script can be inserted anywhere in the aspx page but after the page meta declaration like

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Profile.master.cs" Inherits="OOSDDemo.Profile" %>

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %> (For WebForm)

Solution 6 - asp.net

<div>
	<% 
		if (true)
		{
	%>
	<div>
		Show true content
	</div>
	<%
		}
		else
		{
	%>
	<div>
		Show false content
	</div>
	<%
		}
	%>
</div>

Solution 7 - asp.net

Here's a simple one written in VB for an ASPX page:

                If myVar > 1 Then
                    response.write("Greater than 1")
                else
                    response.write("Not!")
                End If

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBradView Question on Stackoverflow
Solution 1 - asp.netKris van der MastView Answer on Stackoverflow
Solution 2 - asp.netعثمان غنيView Answer on Stackoverflow
Solution 3 - asp.netDaniel DiPaoloView Answer on Stackoverflow
Solution 4 - asp.netMartin PatsovView Answer on Stackoverflow
Solution 5 - asp.netSajeeb Chandan SahaView Answer on Stackoverflow
Solution 6 - asp.netSeattle LeonardView Answer on Stackoverflow
Solution 7 - asp.netTom GullenView Answer on Stackoverflow