Conditionals in ASP.NET can be written in a concise manner using an

I am looking to create a custom statement similar to the following:

<%#((bool)Eval("inactive")) ? "<span style='color:red;font-weight:600;'>INACTIVE</span>" : "<span style='color:green;font-weight:600;'>ACTIVE</span>"%>  

However, I need this statement to be able to handle 3 conditional statements.

So instead of using

<%#Eval("Program_Num") %>  

I want it to output as follows:

  • If Program_Num equals 1, then display X
  • If Program_Num equals 2, then show Y
  • If Program_Num equals 3, then display Z

I am willing to provide further clarification if needed. Thank you for your assistance.

Answer №1

Consider attempting something along these lines.

<%# (int)Eval("Program_Num") == 1 ? "X" : (int)Eval("Program_Num") == 2 ? "Y" : (int)Eval("Program_Num") == 3 ? "Z" : "default value" %>

This can be compared to the following explanation.

if ((int)Eval("Program_Num") == 1)
    "X"
else if ((int)Eval("Program_Num") == 2)
    "Y"
else if ((int)Eval("Program_Num") == 3)
    "Z"
else
    "default value"

Answer №2

Consider attempting the code snippet below:

<%#Eval("Program_Num") == 1 ? "A" : Eval("Program_Num") == 2 ? "B" : Eval("Program_Num") == 3 ? "C" %> 

Remember to replace "A" "B" "C" with your desired values for display.

Answer №3

While this method may not be the most elegant solution, you can achieve your desired outcome by incorporating a nested version of it:

let x = Program_Num === 1 ? "X" : (Program_Num === 2 ? "x" : (Program_Num === 3 ? "y" : "DEFAULT"))

This example is written in clean JavaScript syntax for better understanding, but feel free to modify it for use in an ASP.NET environment.

If you find yourself nesting statements for multiple levels, it may be beneficial to consider utilizing proper if or switch constructs for improved readability and maintainability.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing my slideshow to loop continuously when I click the next button, but not when I click the previous button

I managed to successfully code buttons that control an auto-advancing slideshow. While both buttons are functional, only the next button is able to cycle through all images. However, when using the previous button to navigate back to the beginning, the who ...

How to Unravel Deeply Nested Objects and Arrays in JavaScript

As a newcomer to JavaScript, I am facing the challenge of flattening nested objects and arrays in my code. One of the complex structures I have to work with is: [{ a: 2, b: [{ c: 3, d: [{e: 4, f: 5}, {e: 5,f: 6}]}, { c: 4, d: [{e: 7, ...

prevent parent jQuery functions from interfering with child function execution

I am facing an issue with conflicting jQuery functions between parent and child elements. The child function is a bootstrap function, while the parent is a custom function. The main objective is to limit the height of the parent div (refer to the code belo ...

Log in to the website using your windows username and password

As a beginner in the field of industrial automation development, I am currently working with Siemens. I am interested in utilizing the Windows login username and password for website login through ASP.NET. I would greatly appreciate any guidance on wheth ...

Selecting meteor.js user logout functionality

Is there a reliable method for detecting when a user logs out of the website? I have some maintenance tasks that need to be handled upon logout. The website is built using meteor.js user accounts. I will also be implementing validation features, so it&apo ...

When ColorField is modified, the color of ScatterLineSeries in Telerik UI for ASP.NET AJAX remains unchanged

I am attempting to customize the color of a ScatterLineSeries in Telerik's RadHtmlChart, but I am facing difficulties as the markers and lines are always displayed with default colors. Despite rewriting my code in different styles, I have not been suc ...

Using Angular 2 to dynamically bind attribute names

I am facing an issue with a code block in Angular 2. Here is the code: <div *ngFor="let elm of elms; let i = index" [attr.name]="name" text-center> {{elm }} </div> Everything works well. However, I encountered a problem when trying to ...

How to efficiently manage a complex c# project

My team faces a challenge with our large solution that is built daily in TFS. This solution encompasses multiple logical sub-solutions, such as ApplicationA (projects A,B,C,D), ApplicationB (projects A,B,E,F), and ApplicationC (projects A,C,G,H). Currentl ...

Triggering a Javascript Alert Causes a Webpage Redirection

<?php session_start(); include_once "connect.php"; $fullName = $_POST['fullname']; $userName = $_POST['username']; $emailAdd = $_POST['email']; $passWord = $_POST['password']; $query = mysql_query("SELECT * FR ...

Using a Bookmarklet to Share Images with an Iframe

I am in the final stages of completing a bookmarklet I have been working on. I found helpful information from this stackoverflow thread. A big thank you to Treffynnon for that. So far, I have managed to fetch the title of the current page and pass it to ...

Issue with Material UI: Unable to utilize import statement outside of a module due to Select dependency

Hello there! Here is my query: I am currently working on a project using NextJS + React with node. Everything seems to be running smoothly, except for one issue I encounter when reloading a page with a Select component from Material UI. The relevant code ...

What's the best way to wrap elements in a div dynamically, inside another div?

I am trying to modify the output of WordPress content, but I do not have direct access to edit it. My goal is to use jQuery to wrap each link with accompanying text and line breaks in a div element and then organize them into two columns. The desired final ...

Dapper (a library for .NET) is failing to replace a parameter when attempting to execute a database query

So, when I run this SQL query with Dapper, it ends up not substituting the parameter as expected. Instead, it just writes the raw query to the database like this: https://i.sstatic.net/HKXQs.png (or for some obfuscated exact sql) INSERT INTO Blah SEL ...

Retrieving a collection from a class using Selenium in C#

I have a challenge where I need to extract specific information from a website chart and organize it into a list. Here is the HTML structure I am dealing with: <div AAA class="body"> <div BBB class="id"> <span CCC> 123456 </ ...

Finding numerous keywords in a given text (Javascript)

Here is the code snippet I'm working with: // Finding multiple keywords within a text // Scenario 1 var inputText = "Hello, My name is @Steve, I love @Bill, happy new year!"; var terms = ["steve"]; var result = inputText.toLowerCase().search([terms]) ...

Learn the method of showcasing a JavaScript variable value within an HTML href tag

Currently, I am in the process of rewriting my URL and category name to be passed into the URL. For this purpose, I am creating a URL friendly string using the following JavaScript function: function getUrlFriendlyString(str) { // convert spaces to &ap ...

Issue connecting PostgreSQL database in .Net

I encountered an error when attempting to connect to my local PostgreSQL database. I am seeking assistance with this issue since I am not familiar with the concept. Below are the code and error details: https://i.sstatic.net/0cVAw.png ...

CryptoJS consistently produces identical hash values for distinct files

Utilizing CryptoJS to generate a hash value for uploaded files has presented me with a challenge. Despite my efforts, all files I upload seem to produce identical hash values. It appears that the issue lies within my "onFileChange" function, but pinpointin ...

Troubleshooting CSS override issues when swapping components in ReactJS

Access.js import React from 'react' export default class Access extends React.Component { componentWillMount(){ import ('./styles/access_page.css'); } .... <Link to="/new-account">Sign Up</Link> } Cr ...

The jQuery remove function will only take effect on the second click following an AJAX request

I'm facing an issue with my jQuery code where two divs contain lists of links, triggering AJAX calls to append JSON data to a separate div. Upon clicking a link, the corresponding link div should hide. There's also a third div with the class "pan ...