ChatGPT解决这个技术问题 Extra ChatGPT

If statement in aspx page

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


T
TylerH

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.


XIII i want to checked a radio button if some variable is true, how to do it .?
sample code is class="Default" />
I tried, but it doesn't work. this is my code <% if (false) { %> <asp:Label ID="lblQuantity" runat="server" Text='<%# Convert.ToDouble(Eval("Quantity")).ToString("#####0") + " " + Eval("unitMsr") %>'>></asp:Label> <% } %> but it still show
@JohnNguyen Can you create a new question for this and make up the code in a more readable fashion?
@JohnNguyen It's because your markups is broken, in the end, here: %>'>> It's one > to much. Hope this hasen't been bothering your for 3 years xD
ع
عثمان غني

Just use simple code

<%
if(condition)
{%>

html code

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

D
Daniel DiPaolo

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.


I concur with Daniel DiPaolo on how best to handle a toggled ASP.NET page display item based upon a true / false variable.
M
Martin Patsov

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


S
Sajeeb Chandan Saha

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)


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

T
Tom Gullen

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

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now