ASP.Net/Language Basics/Logical Opertors

Материал из .Net Framework эксперт
Версия от 11:53, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Logical AND Example (C#)

<%@ Page Language="C#" %>
<script runat="server">
    void Page_Load()
    {
        if (IsPostBack){
           DateTime datInput;
           datInput = Convert.ToDateTime(txtIn.Text);
           if ( datInput>=Convert.ToDateTime("1/1/2005") && 
                datInput<=Convert.ToDateTime("12/31/2005"))
             lblOut.Text = "Date inside year 2005. " + datInput;
        }
    }
</script>
<html>
<head>
    <title>Logical AND Example</title>
</head>
<body>
    <form runat="server">
        Please enter a date, first in 2005. 
        <br />
        Next try a date not in 2005 
        <br />
        <asp:TextBox id="txtIn" runat="server"></asp:TextBox>
        <br />
        <asp:Button id="Button1" runat="server" Text="Submit"></asp:Button>
        <br />
        <asp:Label id="lblOut" runat="server"></asp:Label>
        <br />
    </form>
</body>
</html>



Logical Operators in C#: "and", "or" and "not"

<%@ Page Language="C#" Debug="true" %>
<script runat="server">
    void page_Load()
    {
         if(Page.IsPostBack)
         {
             int number1, number2, number3;
             number1 = Convert.ToInt32(txtNumber1.Text);
             number2 = Convert.ToInt32(txtNumber2.Text);
             number3 = Convert.ToInt32(txtNumber3.Text);
    
             lblExpression.Text = "number1 == 1 || !(number1==2) && !(number2==3)";
    
             if (number1 == 1 || !(number1==2) && !(number2==3))
                 lblResult.Text = "true";
             else
                 lblResult.Text = "false";
         }
    }
</script>
<html>
    <head>
    </head>
    <body>
        <form runat="server">
        Note: only works with integers<br>
        Number1 = <asp:TextBox runat="server" ID="txtNumber1" Text=5 width=30 />
        Number1 = <asp:TextBox runat="server" ID="txtNumber2" Text=2 width=30 />
        Number1 = <asp:TextBox runat="server" ID="txtNumber3" Text=3 width=30 /><br/>
    
        Press to see result<asp:Button runat="server" Text="Submit"/><br/>
        
        Logical expression is:<asp:Label runat="server" ID="lblExpression"/><br/>
        Logical expression result is:<asp:Label runat="server" ID="lblResult"/><br/>
        </form>
    </body>
</html>