在 aspx 页面中的 If 语句

我想写一个基本的 if 语句在我的网站上显示项目1或项目2取决于如果一个变量被设置为真。

我不太熟悉。NET,并且需要一些关于如何在 aspx 页面上使用 if 语句的基本结构的帮助

274748 次浏览

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

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.

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.

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

Just use simple code

<%
if(condition)
{%>


html code


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

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

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)