ASP.Net/Language Basics/If Statement
Содержание
If else Demo (C#)
<source lang="csharp">
<%@ Page Language="C#" %> <script runat="server">
void page_Load() { if(Page.IsPostBack) { if(chkIsMember.Checked==true){ lblOut.Text = "Members get a free ticket"; lblOut.BackColor=System.Drawing.Color.LightPink; } else if(Convert.ToInt32(txtAge.Text)<=18) { lblOut.Text = "Students get a free ticket"; lblOut.BackColor=System.Drawing.Color.LightPink; }else { lblOut.Text = "Price is 500"; lblOut.BackColor=System.Drawing.Color.LightSeaGreen; } } }
</script> <html> <head> </head> <body>
<form runat="server"> Please enter your age <asp:TextBox id="txtAge" runat="server"></asp:TextBox>
Are you a member? <asp:CheckBox id="chkIsMember" runat="server"></asp:CheckBox> <asp:Button id="Button1" runat="server" Text="Submit"></asp:Button>
<asp:Label id="lblOut" runat="server"></asp:Label>
</form>
</body> </html>
</source>
If statement the check the input value from asp:TextBox (VB.net)
<source lang="csharp">
<%@ Page Language="VB" %> <script runat="server">
Sub Reset() Textbox1.Text = "" Textbox2.Text = "0" End Sub Sub Page_Load(sender As Object, e As EventArgs) If Not Page.IsPostback Reset() End If End Sub Sub btnSubmit_Click(sender As Object, e As EventArgs) If cint(textbox2.text) < 20 Then label1.text = "Hi " & textbox1.text & ", you are " & textbox2.text & ", which is young!" Else If cint(textbox2.text) < 40 Then label1.text = "Hi " & textbox1.text & ", you are " & textbox2.text & ", which means you"re middle-aged!" Else label1.text = "Hi " & textbox1.text & ", you"re old!" End If End Sub Sub btnReset_Click(sender As Object, e As EventArgs) Reset() End Sub
</script> <html> <head> </head> <body>
<form runat="server">
Enter your name: <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
Enter you age: <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
<asp:Button id="btnSubmit" onclick="btnSubmit_Click" runat="server" Text="Submit"></asp:Button> <asp:Button id="btnReset" onclick="btnReset_Click" runat="server" Text="Reset"></asp:Button>
<asp:Label id="Label1" runat="server"></asp:Label>
</form>
</body> </html>
</source>
If then Demo (C#)
<source lang="csharp">
<script language="C#" runat="server"> void Page_Load() {
int intNumber, intGuess; Random r = new Random(); intNumber = Convert.ToInt32(r.Next(10)) + 1; if (Page.IsPostBack) { intGuess = Convert.ToInt32(Guess.SelectedItem.Value); if (intGuess > intNumber) { Message.Text = "
Guess is too high
Try again, it was " + intNumber; } if (intGuess < intNumber) { Message.Text = "
Guess is too low
Try again, it was " + intNumber; } if (intGuess == intNumber) { Message.Text = "
Guess is correct!"; } }
} </script> <html> <head></head> <body> <form runat="server">
Guess? <asp:dropdownlist id="Guess" runat="server"> <asp:listitem>1</asp:listitem> <asp:listitem>2</asp:listitem> <asp:listitem>3</asp:listitem> <asp:listitem>4</asp:listitem> <asp:listitem>5</asp:listitem> <asp:listitem>6</asp:listitem> <asp:listitem>7</asp:listitem> <asp:listitem>8</asp:listitem> <asp:listitem>9</asp:listitem> <asp:listitem>10</asp:listitem> </asp:dropdownlist>
<input type="submit" value="Submit guess"> <asp:label id="Message" runat="server"/>
</form> </body> </html>
</source>
Multiple conditions in If statement (VB.net)
<source lang="csharp">
<%@ Page Language="VB" %> <script runat="server">
Sub Calendar1_SelectionChanged(sender As Object, e As EventArgs) If Day (Calendar1.SelectedDate) = 13 And Calendar1.SelectedDate.DayOfWeek = 5 Then Label1.Text = "Careful! Friday 13th is an unlucky day for some people!" Else Label1.Text = "It"s just another day in life..." End If End Sub
</script> <html> <head> </head> <body>
<form runat="server">
Please select a date:
<asp:Calendar id="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
<asp:Label id="Label1" runat="server"></asp:Label>
</form>
</body> </html>
</source>
Nested if statement (C#)
<source lang="csharp">
<script language="C#" runat="server"> void Page_Load() {
int intNumber, intGuess; Random r = new Random(); intNumber = Convert.ToInt32(r.Next(10)) + 1; if (Page.IsPostBack) { intGuess = Convert.ToInt32(Guess.SelectedItem.Value); if (intGuess > intNumber) { Message.Text = "
Guess is too high
Try again, it was " + intNumber; } if (intGuess < intNumber) { Message.Text = "
Guess is too low
Try again, it was " + intNumber; } if (intGuess == intNumber) { Message.Text = "
Guess is correct!"; } }
} </script> <html> <head></head> <body> <form runat="server">
What number am I thinking of? <asp:dropdownlist id="Guess" runat="server"> <asp:listitem>1</asp:listitem> <asp:listitem>2</asp:listitem> <asp:listitem>3</asp:listitem> <asp:listitem>4</asp:listitem> <asp:listitem>5</asp:listitem> <asp:listitem>6</asp:listitem> <asp:listitem>7</asp:listitem> <asp:listitem>8</asp:listitem> <asp:listitem>9</asp:listitem> <asp:listitem>10</asp:listitem> </asp:dropdownlist>
<input type="submit" value="Submit guess"> <asp:label id="Message" runat="server"/>
</form> </body> </html>
</source>
Nested if statement (VB.net)
<source lang="csharp">
<script language="vb" runat="server"> Sub Page_Load()
Dim theNumber As Integer Dim theGuess As Integer theNumber = int(10 * rnd) + 1 If Page.IsPostBack Then theGuess = Guess.SelectedItem.Value If theGuess > theNumber then Message.Text = "
Guess is too high
Try again ?it was " _ & theNumber End If If theGuess < theNumber then Message.Text = "
Guess is too low
Try again ?it was " _ & theNumber End If If theGuess = theNumber then Message.Text = "
Guess is correct!" End If End If
End Sub </script> <html> <head></head> <body> <form runat="server">
What number am I thinking of? <asp:dropdownlist id="Guess" runat="server"> <asp:listitem>1</asp:listitem> <asp:listitem>2</asp:listitem> <asp:listitem>3</asp:listitem> <asp:listitem>4</asp:listitem> <asp:listitem>5</asp:listitem> <asp:listitem>6</asp:listitem> <asp:listitem>7</asp:listitem> <asp:listitem>8</asp:listitem> <asp:listitem>9</asp:listitem> <asp:listitem>10</asp:listitem> </asp:dropdownlist>
<input type="submit" value="Submit guess"> <asp:label id="message" runat="server"/>
</form> </body> </html>
</source>