简体   繁体   English

DropDownLists 问题 ASP.NET

[英]DropDownLists Issue ASP.NET

I really have a serious issues with postback, I have 3 dropdownlists, the first one contain data from a database, it's the countries, the second is the same but for the cities who depends the selected value on the first dropdownlist which is the countries, and the third dropdownlist is the airlines who depends on the selected value of the second dropdownlist which is the cities.我确实有一个严重的回发问题,我有 3 个下拉列表,第一个包含来自数据库的数据,它是国家,第二个是相同的,但对于依赖于第一个下拉列表(国家)的所选值的城市,第三个下拉列表是航空公司,它取决于第二个下拉列表的所选值,即城市。

So the first two dropdownlists work perfectly, but the third dropdownlist will never work even with autopostback true and false, it will always refresh on to the first value and I wrote if(.IsPostback) so I really I'm frustrated about this issue.所以前两个下拉列表工作得很好,但第三个下拉列表永远不会工作,即使使用自动回发 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();
        }
    }
}

Problem is here:问题在这里:

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

You said that the third dropdownlist is the airlines who depends on the selected value of the second dropdownlist which is the cities.你说第三个下拉列表是航空公司,它取决于第二个下拉列表的所选值,即城市。 But in the above code you are selecting the value of third dropdown which makes no sense as it doesn't have any value.但是在上面的代码中,您选择的是第三个下拉列表的值,这没有任何意义,因为它没有任何价值。

So instead of above code, change the id value to this:因此,不要使用上面的代码,而是将 id 值更改为:

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

Check this and let me know.检查这个,让我知道。

Your problem is permanently recreate ddl.您的问题是永久重新创建 ddl。

 gather_Tocountries(); gather_Tocities();

Well, for solution this problem, you need after created your ddls set selected value for ToCit and To ddl.那么,为了解决这个问题,您需要在创建您的 ddls 后为 ToCit 和 To ddl 设置选定的值。

ToCit.SelectedValue = yourValue; //Something like this;

But code which you wrote is not productive, because you each time call database但是您编写的代码效率不高,因为您每次都调用数据库

if you wanna init your ddl after select in prev, just call your method CountriesAndCities in SelectedIndexChanged.如果您想在上一个选择后初始化您的 ddl,只需在 SelectedIndexChanged 中调用您的方法CountriesAndCities First init you can do in pageInit or pageLoad but with use if(isPostBack){your first ddl init}您可以在pageInitpageLoad中执行第一个初始化,但使用if(isPostBack){your first ddl init}

for example例如

PageLoad(){
  if(IsPostBack){
   string Countries = "select * from Countries order by country desc";
            CountriesAndCities(Countries, FromCount);
 }
}

For other ddl the same对于其他ddl相同

then然后

protected void FromCount_SelectedIndexChanged(object sender, EventArgs e)
{
    gather_cities();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM