ASP.Net/Language Basics/If Statement
Содержание
If else Demo (C#)
<%@ 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>
<br />
<form runat="server">
Please enter your age
<asp:TextBox id="txtAge" runat="server"></asp:TextBox>
<br />
Are you a member?
<asp:CheckBox id="chkIsMember" runat="server"></asp:CheckBox>
<asp:Button id="Button1" runat="server" Text="Submit"></asp:Button>
<br />
<asp:Label id="lblOut" runat="server"></asp:Label>
<br />
</form>
</body>
</html>
If statement the check the input value from asp:TextBox (VB.net)
<%@ 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">
<p>
Enter your name:
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</p>
<p>
Enter you age:
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
</p>
<p>
<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>
</p>
<p>
<asp:Label id="Label1" runat="server"></asp:Label>
</p>
</form>
</body>
</html>
If then Demo (C#)
<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 = "<BR><BR>Guess is too high<BR>Try again, it was "
+ intNumber;
}
if (intGuess < intNumber) {
Message.Text = "<BR><BR>Guess is too low<BR>Try again, it was "
+ intNumber;
}
if (intGuess == intNumber) {
Message.Text = "<BR><BR>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>
<br>
<br>
<input type="submit" value="Submit guess">
<asp:label id="Message" runat="server"/>
</form>
</body>
</html>
Multiple conditions in If statement (VB.net)
<%@ 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">
<p>
Please select a date:
</p>
<p>
<asp:Calendar id="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
</p>
<p>
<asp:Label id="Label1" runat="server"></asp:Label>
</p>
</form>
</body>
</html>
Nested if statement (C#)
<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 = "<BR><BR>Guess is too high<BR>Try again, it was "
+ intNumber;
}
if (intGuess < intNumber) {
Message.Text = "<BR><BR>Guess is too low<BR>Try again, it was "
+ intNumber;
}
if (intGuess == intNumber) {
Message.Text = "<BR><BR>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>
<br>
<br>
<input type="submit" value="Submit guess">
<asp:label id="Message" runat="server"/>
</form>
</body>
</html>
Nested if statement (VB.net)
<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 = "<BR><BR>Guess is too high<BR>Try again ?it was " _
& theNumber
End If
If theGuess < theNumber then
Message.Text = "<BR><BR>Guess is too low<BR>Try again ?it was " _
& theNumber
End If
If theGuess = theNumber then
Message.Text = "<BR><BR>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>
<br>
<br>
<input type="submit" value="Submit guess">
<asp:label id="message" runat="server"/>
</form>
</body>
</html>