İl’e göre İlçe , İlçeye göre Mahalle Listele — Asp Net — C#


[code lang=”html”]
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 142px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td class="auto-style2">
İl —————-></td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="IlAdi" DataValueField="IlID" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>" ProviderName="<%$ ConnectionStrings:ConnectionString2.ProviderName %>" SelectCommand="SELECT [IlID], [IlAdi] FROM [Iller]"></asp:SqlDataSource>
</td>
</tr>
<tr>
<td class="auto-style2">
İlçe ————–></td>
<td>
<asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="auto-style2">
Mahalle ———-></td>
<td>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True">
</asp:DropDownList>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
[/code]
[code lang=”csharp”]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;//www.gorselprogramlama.com
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
DropDownList2.Items.Clear();//www.gorselprogramlama.com
Session["ilID"] = Convert.ToInt32(DropDownList1.SelectedValue);
OleDbConnection bag = new OleDbConnection("provider=microsoft.ace.oledb.12.0;data source=" + Server.MapPath("data.accdb"));
OleDbCommand kmt = new OleDbCommand();
kmt.Connection = bag;
kmt.CommandText = "select * from Ilceler where IlID=@IlID";
kmt.Parameters.AddWithValue("@IlID", Session["ilID"]);
OleDbDataReader oku;
bag.Open();
oku = kmt.ExecuteReader();
DropDownList2.DataSource = oku;
DropDownList2.DataValueField = "IlceID";
DropDownList2.DataTextField = "IlceAdi";
DropDownList2.DataBind();
bag.Close();
}
catch (Exception)
{
}
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
DropDownList3.Items.Clear();
Session["ilceID"] = Convert.ToInt32(DropDownList2.SelectedValue);
OleDbConnection bag = new OleDbConnection("provider=microsoft.ace.oledb.12.0;data source=" + Server.MapPath("data.accdb"));
OleDbCommand kmt = new OleDbCommand();
kmt.Connection = bag;
kmt.CommandText = "select * from Mahalleler where IlID=@IlID and IlceID=@IlceID";
kmt.Parameters.AddWithValue("@IlID", Session["ilID"]);
kmt.Parameters.AddWithValue("@IlceID", Session["ilceID"]);
OleDbDataReader oku;
bag.Open();
oku = kmt.ExecuteReader();
DropDownList3.DataSource = oku;
DropDownList3.DataValueField = "MahID";
DropDownList3.DataTextField = "MahAdi";
DropDownList3.DataBind();//www.gorselprogramlama.com
bag.Close();
}
catch (Exception)
{
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}<a href="https://www.gorselprogramlama.com/wp-content/uploads/2017/11/il_ilce_mahalle.jpg">
</a>
[/code]