switch - case kullanımı detaylı .NET NOTLARIM
07 Aralık 2017 Bu içerik 2.264 kez okundu.
switch - case kullanımı detaylı .NET NOTLARIM
switch - case ile div gizleme ve açma,
ÖNEMLİ NOT:
<asp:DropDownList ID="ddlSekil" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlSekil_SelectedIndexChanged">
BU ŞEKİLDE RUNAT SERVER AÇILMASI GEREKİYOR VE AUTOPOSTBACK TRUE OLMASI GEREKLİYOR.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlSekil" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlSekil_SelectedIndexChanged">
<asp:ListItem Value="0">Kare</asp:ListItem>
<asp:ListItem Value="1">Diktörgen</asp:ListItem>
<asp:ListItem Value="2">Üçgen</asp:ListItem>
</asp:DropDownList>
<div runat="server" id="kare" visible="false">
<asp:Label ID="Label1" runat="server" Text="Kenar Uzunluğu :"></asp:Label>
<asp:TextBox ID="txtKareKenar" runat="server"></asp:TextBox>
</div>
<div runat="server" id="Diktortgen" visible="false">
<asp:Label ID="Label2" runat="server" Text="Kısa Kenar Uzunluğu :"></asp:Label>
<asp:TextBox ID="txtKisaKenar" runat="server"></asp:TextBox><br />
<asp:Label ID="Label3" runat="server" Text="Uzun Kenar Uzunluğu :"></asp:Label>
<asp:TextBox ID="txtUzunKenar" runat="server"></asp:TextBox>
</div>
<div runat="server" id="Ucgen" visible="false">
<asp:Label ID="Label4" runat="server" Text="Taban Alanı :"></asp:Label>
<asp:TextBox ID="txtTabanalani" runat="server"></asp:TextBox><br />
<asp:Label ID="Label5" runat="server" Text="Yükseklik :"></asp:Label>
<asp:TextBox ID="txtYukseklik" runat="server"></asp:TextBox>
</div>
<asp:Button ID="btnGonder" runat="server" Text="Hesapla" Visible="false" OnClick="btnGonder_Click" />
</div>
</form>
</body>
</html>
-----------------
ÖNCELİKLE SELECTVALUE için switch case yazılır, daha sonra button için yazılır.
ÖNEMLİ NOT: SELECTVALUE İÇİN SERVERA GÖNDERME YANİ AUTOPOSTBACK AÇIK OLMASI GEREKİYOR.
protected void ddlSekil_SelectedIndexChanged(object sender, EventArgs e)
{
string seciliveri = ddlSekil.SelectedValue;
switch(seciliveri)
{
case "0":
kare.Visible = true;
btnGonder.Visible = true;
Diktortgen.Visible = false;
Ucgen.Visible = false;
break;
case "1":
Diktortgen.Visible = true;
btnGonder.Visible = true;
kare.Visible = false;
Ucgen.Visible = false;
break;
case "2":
Ucgen.Visible = true;
btnGonder.Visible = true;
Diktortgen.Visible = false;
kare.Visible = false;
break;
}
}
protected void btnGonder_Click(object sender, EventArgs e)
{
string seciliveri = ddlSekil.SelectedValue;
int alan=0, cevre=0;
switch(seciliveri)
{
case "0":
alan = Convert.ToInt32(txtKareKenar.Text) * Convert.ToInt32(txtKareKenar.Text);
cevre = 4 * Convert.ToInt32(txtKareKenar.Text);
break;
case "1":
alan = Convert.ToInt32(txtKisaKenar.Text) * Convert.ToInt32(txtUzunKenar.Text);
cevre = 2 * (Convert.ToInt32(txtKisaKenar.Text) + Convert.ToInt32(txtUzunKenar.Text)) ;
break;
case "2":
alan = Convert.ToInt32(txtTabanalani.Text) * Convert.ToInt32(txtYukseklik.Text);
cevre = 3 * Convert.ToInt32(txtTabanalani.Text);
break;
}
Response.Write(string.Format("Alan={0}</br> Çevre={1}", alan, cevre));
}
}
}