Ternary Operator Using in Asp.Net C#

                  Ternary Operator 

Ternary operator  used to easy way of  checking condition instead of if  condition using in Asp.Net C#.

                                       ? - True Condition
                          : - False Condition

                                 DEMO 


                                               

                               Download                             

                           HTML CODING

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Ternary Operator Using in Asp.Net C#</title>
</head>
<body>
    <form id="form1" runat="server">
    <div >
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Username"></asp:Label>
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>

        <br />
        <asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>
 <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Login" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>


           C# CODING

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class TernaryOperatorr : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
        // Ternary Operator

        

        var mm = (txtUsername.Text == "admin" ? " Username Valid" : " Username Invalid" );
        var mmm =( txtPassword.Text=="admin" ? "Password Valid" : "Password Invalid"  );

        Response.Write(mm.ToString() +","+mmm.ToString());
    }

}



First Create new Webform - add Textbox & buttons 

from toolbox





Next - Codebehind  use ternary operator  for simple static login condition
? - True Condition
: - False Condition


0 comments:

Post a Comment