级联下拉列表在使用c#的asp.net中不起作用。(不使用ajax) [英] Cascading dropdownlist does not work in asp.net using c#.(not using ajax)
本文介绍了级联下拉列表在使用c#的asp.net中不起作用。(不使用ajax)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<asp:DropDownList ID="countrylist" runat="server"
onselectedindexchanged="countrylist_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style5">
state</td>
<td class="style6">
<asp:DropDownList ID="statelist" runat="server"
onselectedindexchanged="statelist_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style2">
City</td>
<td>
<asp:DropDownList ID="city" runat="server"
onselectedindexchanged="city_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>
namespace tables
{
public partial class CompanyRegi : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Contrydropdown();
}
}
protected void Contrydropdown()
{
//conenction path for database
SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");
con.Open();
SqlCommand cmd = new SqlCommand("select * from Country", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
countrylist.DataSource = ds;
countrylist.DataTextField = "CountryName";
countrylist.DataValueField = "CountryID";
countrylist.DataBind();
countrylist.Items.Insert(0, new ListItem("--Select--", "0"));
statelist.Items.Insert(0, new ListItem("--Select--", "0"));
city.Items.Insert(0, new ListItem("--Select--", "0"));
con.Close();
}
/// Bind State Dropdown Based on CountryID
protected void countrylist_SelectedIndexChanged(object sender, EventArgs e)
{
int CountryID = Convert.ToInt32(countrylist.SelectedValue);
SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");
con.Open();
SqlCommand cmd = new SqlCommand("select * from State where CountryID=" + CountryID, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
statelist.DataSource = ds;
statelist.DataTextField = "StateName";
statelist.DataValueField = "StateID";
statelist.DataBind();
statelist.Items.Insert(0, new ListItem("--Select--", "0"));
con.Close();
if (statelist.SelectedValue == "0")
{
city.Items.Clear();
city.Items.Insert(0, new ListItem("--Select--", "0"));
}
}
protected void statelist_SelectedIndexChanged(object sender, EventArgs e)
{
int StateID = Convert.ToInt32(statelist.SelectedValue);
SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");
con.Open();
SqlCommand cmd = new SqlCommand("select * from City where StateID=" + StateID, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
city.DataSource = ds;
city.DataTextField = "CityName";
city.DataValueField = "CityID";
city.DataBind();
city.Items.Insert(0, new ListItem("--Select--", "0"));
}
protected void city_SelectedIndexChanged(object sender, EventArgs e)
{
}
推荐答案
这里是更新面板替代例示
Here's the update panel alternative examplified
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SampleCascade.aspx.cs" Inherits="Lunch.SampleCascade" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="AjaxScriptManager" EnablePartialRendering="True"></asp:ScriptManager>
<div>
<asp:UpdatePanel runat="server" ID="UpdPnl" UpdateMode="Always">
<ContentTemplate>
<div>
<label>Country</label>
<asp:DropDownList runat="server" ID="CountryDropDown" AutoPostBack="True" OnSelectedIndexChanged="CountryDropDownSelectedIndexChanged">
<asp:ListItem Value="dk" Selected="True">Denmark</asp:ListItem>
<asp:ListItem Value="ca">Canada</asp:ListItem>
</asp:DropDownList>
</div>
<div>
<label>City</label>
<asp:DropDownList runat="server" ID="CityDropDown" AutoPostBack="True" OnSelectedIndexChanged="CityDropDownSelectedIndexChanged">
</asp:DropDownList>
</div>
<asp:Label runat="server" ID="Feedback"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
并且代码背后
and with code behind
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
namespace Lunch
{
public partial class SampleCascade : System.Web.UI.Page
{
private Dictionary<string, HashSet<string>> _cities = new Dictionary<string, HashSet<string>>();
protected void Page_Init(object sender, EventArgs e)
{
_cities.Add("dk", new HashSet<string>(new []{ "Copenhagen", "Aalborg", "Odense" }));
_cities.Add("ca", new HashSet<string>(new [] { "Ottawa", "Toronto", "Charlottetown" }));
}
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
LoadCities(CountryDropDown.SelectedValue);
}
protected void CountryDropDownSelectedIndexChanged(object sender, EventArgs e)
{
var ctry = (DropDownList) sender;
LoadCities(ctry.Items[ctry.SelectedIndex].Value);
Feedback.Text = "Loaded cities for " + ctry.Items[ctry.SelectedIndex].Text;
}
protected void CityDropDownSelectedIndexChanged(object sender, EventArgs e)
{
var city = (DropDownList) sender;
ListItem selected = city.Items[city.SelectedIndex];
if (selected.Value == "0") return;
Feedback.Text = "City selected: " + selected.Text;
}
private void LoadCities(string country)
{
CityDropDown.Items.Clear();
foreach (var itm in _cities[country])
{
CityDropDown.Items.Add(new ListItem(itm, itm.ToLower()));
}
CityDropDown.Items.Insert(0, new ListItem("Select", "0"));
}
}
}
这篇关于级联下拉列表在使用c#的asp.net中不起作用。(不使用ajax)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文