If Statement condition in ASP.NET Web-Forms?


I want to write a basic if statement on my web-application, as I would like to place <script> based on a condition in .aspx page, how can I achieve it?

Something like

if(Condition)
{
   //script here
}
else
{
}

here is my script, that i want to place

<script type="text/javascript">

            function openColorBox() {

                $.colorbox({

                    innerWidth:400, 

                    innerHeight:300, 

                    iframe:true,

                    href: "rating_page.aspx",

                    overlayClose:true,

                    onLoad: function() {

                        // $('#cboxClose').remove();

                        //  $.colorbox({ overlayClose: true });

                    }


        });

      }

     //  openColorBox.Stop()

      setTimeout(openColorBox, 1000);

    </script>

Asked by:- RameshwarLate
3
: 6527 At:- 8/30/2017 3:16:22 PM
asp.net web-forms if-statement







3 Answers
profileImage Answered by:- vikas_jk

You can place if-condition in .aspx pages using the code below

<%
if(condition)
{%>

//Your script or html here 

<% } 
else 
{
%>
//Your script or html here 
<% } %>

I hope this helps, thanks

2
At:- 8/30/2017 3:26:09 PM


profileImage Answered by:- pika

Check this code , it may help you

.aspx code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Example Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <%if (i == 1) { %>
            <asp:Label ID="Label1" runat="server" Text="If Bloc"></asp:Label><br />
           //Script code here           
            <%} %>
            <%else { %>
            <asp:Label ID="Label2" runat="server" Text="Else Block"></asp:Label>
             //Script code here
            <%} %>
        </div>
    </form>
</body>
</html>

C# Code in code behind

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    public int i;
    protected void Page_Load(object sender, EventArgs e)
    {
        i = 1;
       
    }
}
1
At:- 9/4/2017 7:50:19 AM Updated at:- 9/4/2017 7:52:27 AM


profileImage Answered by:- bhanu

You can simply wrap markup in a condition as below

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

OR

On page load check this

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

These are two easy solutions.

0
At:- 11/15/2021 6:59:59 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use