[英]DropDownLists Issue ASP.NET
我確實有一個嚴重的回發問題,我有 3 個下拉列表,第一個包含來自數據庫的數據,它是國家,第二個是相同的,但對於依賴於第一個下拉列表(國家)的所選值的城市,第三個下拉列表是航空公司,它取決於第二個下拉列表的所選值,即城市。
所以前兩個下拉列表工作得很好,但第三個下拉列表永遠不會工作,即使使用自動回發 true 和 false,它總是會刷新到第一個值,我寫了 if(.IsPostback) 所以我真的對這個問題感到沮喪。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.IO;
namespace Hijazi_Airlines
{
public partial class Book : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["HAirlines"].ConnectionString);
protected void Page_Init(object sender, EventArgs e)
{
if (Session["Username"] != null)
{
Sign.Text = "Sign Out";
}
else
{
}
gather_countries();
gather_cities();
gather_Tocountries();
gather_Tocities();
}
private void gather_date()
{
try
{
string query = "SELECT Depart FROM Flights where Airlines_id=" + Airlin.SelectedValue;
SqlCommand sqlcmd = new SqlCommand(query, sqlcon);
sqlcon.Open();
SqlDataReader reader = sqlcmd.ExecuteReader();
reader.Read();
Response.Write(reader[0].ToString());
sqlcon.Close();
}
catch
{
}
sqlcon.Close();
}
private void gather_cities()
{
FromCit.Items.Clear();
string cities = "select * from Cities where country_id = " + FromCount.SelectedValue;
CountriesAndCities(cities, FromCit);
}
private void gather_Tocities()
{
ToCit.Items.Clear();
string cities = "select * from Cities where country_id = " + To.SelectedValue;
CountriesAndCities(cities, ToCit);
}
private void gather_countries()
{
string Countries = "select * from Countries order by country desc";
CountriesAndCities(Countries, FromCount);
}
private void gather_Tocountries()
{
string Countries = "select * from Countries order by country desc";
CountriesAndCities(Countries, To);
}
private void CountriesAndCities(string query, DropDownList dp)
{
SqlCommand sqlcmdC = new SqlCommand(query, sqlcon);
sqlcon.Open();
SqlDataReader reader = sqlcmdC.ExecuteReader();
while (reader.Read())
{
ListItem item = new ListItem();
item.Text = reader[1].ToString();
item.Value = reader[0].ToString();
dp.Items.Insert(0, item);
}
sqlcon.Close();
}
protected void Hom_Click(object sender, EventArgs e)
{
Response.Redirect("Home.aspx");
}
protected void SignIN_Click1(object sender, EventArgs e)
{
if (Sign.Text == "Sign Out")
{
Session.RemoveAll();
Session.Abandon();
Sign.Text = "Sign In";
}
else
{
Response.Redirect("Login.aspx");
}
}
protected void Contact_Click(object sender, EventArgs e)
{
Response.Redirect("Contact.aspx");
}
protected void FromCount_SelectedIndexChanged(object sender, EventArgs e)
{
gather_cities();
}
protected void To_SelectedIndexChanged(object sender, EventArgs e)
{
gather_Tocities();
}
protected void Airlin_SelectedIndexChanged(object sender, EventArgs e)
{
gather_date();
}
}
}
問題在這里:
string query = "SELECT Depart FROM Flights where Airlines_id=" + Airlin.SelectedValue;
你說第三個下拉列表是航空公司,它取決於第二個下拉列表的所選值,即城市。 但是在上面的代碼中,您選擇的是第三個下拉列表的值,這沒有任何意義,因為它沒有任何價值。
因此,不要使用上面的代碼,而是將 id 值更改為:
string query = "SELECT Depart FROM Flights where Airlines_id=" + YourCityDropDownId.SelectedValue;
檢查這個,讓我知道。
您的問題是永久重新創建 ddl。
gather_Tocountries(); gather_Tocities();
那么,為了解決這個問題,您需要在創建您的 ddls 后為 ToCit 和 To ddl 設置選定的值。
ToCit.SelectedValue = yourValue; //Something like this;
但是您編寫的代碼效率不高,因為您每次都調用數據庫
如果您想在上一個選擇后初始化您的 ddl,只需在 SelectedIndexChanged 中調用您的方法CountriesAndCities 。 您可以在pageInit或pageLoad中執行第一個初始化,但使用if(isPostBack){your first ddl init}
例如
PageLoad(){
if(IsPostBack){
string Countries = "select * from Countries order by country desc";
CountriesAndCities(Countries, FromCount);
}
}
對於其他ddl相同
然后
protected void FromCount_SelectedIndexChanged(object sender, EventArgs e)
{
gather_cities();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.