C# DropDownLists发布ASP.NET

C# DropDownLists发布ASP.NET,c#,asp.net,drop-down-menu,C#,Asp.net,Drop Down Menu,我真的有一个严重的回发问题,我有3个下拉列表,第一个包含来自数据库的数据,是国家,第二个是相同的,但对于城市来说,选择的值取决于第一个下拉列表,即国家,第三个下拉列表是航空公司,取决于第二个下拉列表的选定值,即城市 所以前两个dropdownlist工作得很好,但是第三个dropdownlist即使使用autopostback true和false也不会工作,它总是刷新到第一个值,我写了if(!IsPostback),所以我真的对这个问题感到失望 using System; using Syst

我真的有一个严重的回发问题,我有3个下拉列表,第一个包含来自数据库的数据,是国家,第二个是相同的,但对于城市来说,选择的值取决于第一个下拉列表,即国家,第三个下拉列表是航空公司,取决于第二个下拉列表的选定值,即城市

所以前两个dropdownlist工作得很好,但是第三个dropdownlist即使使用autopostback 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;
检查此项并让我知道。

问题在这里:

string query = "SELECT Depart FROM Flights where Airlines_id=" + Airlin.SelectedValue;
您说过,第三个下拉列表是航空公司,取决于第二个下拉列表的选定值,即城市。但是在上面的代码中,您选择的是第三个下拉列表的值,这没有任何意义,因为它没有任何值

因此,将id值更改为以下值,而不是上面的代码:

string query = "SELECT Depart FROM Flights where Airlines_id=" + YourCityDropDownId.SelectedValue;

检查此项并通知我。

您的问题是永久重新创建ddl

好的,为了解决这个问题,您需要在创建ddl后为ToCit和toDDL设置所选的值

ToCit.SelectedValue = yourValue; //Something like this;
但您编写的代码并没有产生效果,因为您每次都调用数据库

如果您想在prev中选择后初始化ddl,只需调用您的方法 所选索引中的国家和城市已更改。第一次初始化您可以在pageInitpageLoad中执行,但使用
if(isPostBack){您的第一次ddl初始化}

比如说

PageLoad(){
  if(IsPostBack){
   string Countries = "select * from Countries order by country desc";
            CountriesAndCities(Countries, FromCount);
 }
}
对于其他ddl,相同

然后


您的问题是永久重新创建ddl

好的,为了解决这个问题,您需要在创建ddl后为ToCit和toDDL设置所选的值

ToCit.SelectedValue = yourValue; //Something like this;
但您编写的代码并没有产生效果,因为您每次都调用数据库

如果您想在prev中选择后初始化ddl,只需调用您的方法 所选索引中的国家和城市已更改。第一次初始化您可以在pageInitpageLoad中执行,但使用
if(isPostBack){您的第一次ddl初始化}

比如说

PageLoad(){
  if(IsPostBack){
   string Countries = "select * from Countries order by country desc";
            CountriesAndCities(Countries, FromCount);
 }
}
对于其他ddl,相同

然后


我相信这就是你想要的:使用
if(!IsPostBack)
,你不需要使用
Page\u Init
Page\u Load
更好。我已经使用了
if(!IsPostBack)
,但它不起作用,对于Page\u Load,没有任何东西能起作用:/一种避免使用回发的方法是使用ajax。在你的aspx文件中创建一个javascript脚本来操作这个下拉列表。我相信这就是你想要的:使用
if(!IsPostBack)
,你不需要使用
Page\u Init
Page\u Load
更好。我已经使用了
if(!IsPostBack)
,它不起作用,对于Page\u Load也不起作用,没有任何效果:/a避免使用回发的一种方法是使用ajax。在你的aspx文件中创建一个javascript脚本来处理这个下拉列表。您好,谢谢您的回答,我已经解决了这个问题,但是在选择时,航空公司的DropDownList总是返回第一个值another@Alou4a从您编写的方法gather_cities()中,FromCount来自何处?这些是其他dropdownlist,FromCount,FromCit是另外两个dropdownlist,但级联的三个dropdownlist是To,它是To Country dropdownlist,ToIt wish是To City dropdownlist,Airlin是Airlines dropdownlist。代码看起来不错,应该可以工作。我希望这个链接会有帮助:您好,谢谢您的回答,我修正了这个问题,但是在选择时,航空公司的下拉列表总是返回第一个值another@Alou4a从您编写的方法gather_cities()中,FromCount从何而来?这些是其他dropdownlist,FromCount,FromCity,是其他两个dropdownlist,但是级联的三个Dropdownlist是To,它是To Country Dropdownlist,ToCit wish是To City Dropdownlist,Airlin是Airlines Dropdownlist。嗯,代码看起来不错,应该可以工作。我希望这个链接能有所帮助:我总共有6个ddl,2个ddl,从一个国家到另一个国家,另外2个ddl从城市到另一个城市,另外2个ddl,一个是航空公司,一个是酒店,第一个ddl从一个国家到另一个国家和城市,之后,航空公司的ddl从一个城市到另一个城市,同样是酒店,最后,当用户从国家中选择一个国家时,他将从城市中获得该城市,与从国家到国家一样,他将到达城市,当他从城市中选择一个城市时,他将从城市中选择该城市的航空公司,这就是为什么我重新创建了ddlsi,共有6个ddls,2个ddls,从国家到国家,2个来自城市的其他ddls,To City和其他两个ddl,一个是航空公司,一个是酒店,第一个来自国家/地区的ddl与From City绑定,与To Country和To City相同,之后,航空公司的ddl与To City绑定,与hotel相同,因此最后当用户在From Country中选择一个国家/地区时,他将从城市中获取城市,与To Country相同,他将到达城市,当他从城市中选择一个城市时,他将在城市中选择城市的航空公司,这就是我重新创建DDL的原因