【问题标题】:DropDownLists Issue ASP.NETDropDownLists 问题 ASP.NET
【发布时间】:2019-12-12 14:34:48
【问题描述】:

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

所以前两个下拉列表完美地工作,但第三个下拉列表将永远无法工作,即使 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();
        }
    }
}

【问题讨论】:

  • 我相信这就是你要找的东西:c-sharpcorner.com/UploadFile/8a67c0/…
  • 使用if (!IsPostBack),而且不用Page_InitPage_Load更好。
  • 我已经使用了if(!IsPostBack),但它不能正常工作,对于 Page_Load 也一样,什么都不能工作:/
  • 一种避免使用回发的方法是使用 ajax。在您的 aspx 文件中创建一个 javascript 脚本来操作此下拉列表。

标签: c# asp.net drop-down-menu


【解决方案1】:

问题就在这里:

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

您说第三个下拉列表是航空公司,它取决于第二个下拉列表的选定值,即城市。但是在上面的代码中,您选择的是第三个下拉列表的值,这没有任何意义,因为它没有任何值。

所以代替上面的代码,将 id 值更改为:

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

检查一下并告诉我。

【讨论】:

  • 您好,谢谢您的回答,我已经解决了这个问题,但是航空公司的 DropDownList 在选择另一个时总是返回第一个值
  • @Alou4a 来自您编写的方法 gather_cities() FromCount 来自哪里?
  • 这些是其他下拉列表,FromCount、FromCit 是另外两个下拉列表,但级联的三个下拉列表是 To,即 To Country Dropdownlist,ToCit wish 是 To City 下拉列表,而 Airlin 是航空公司 DropDownLIST。
  • 好吧,代码看起来不错,应该可以工作。我希望这个链接会有所帮助:c-sharpcorner.com/UploadFile/009464/…
【解决方案2】:

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

        gather_Tocountries();
        gather_Tocities();

好吧,为了解决这个问题,您需要在创建 ddls 后为 ToCit 和 To ddl 设置选定的值。

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

但是你写的代码效率不高,因为你每次都调用数据库

如果你想在 prev 中选择后初始化你的 ddl,只需调用你的方法 CountriesAndCities 在 SelectedIndexChanged 中。您可以在 pageInitpageLoad 中进行首次初始化,但使用 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();
}

【讨论】:

  • 我总共有 6 个 ddls,2 个 ddls,从国家到国家,2 个其他 ddl 从城市到城市,还有 2 个其他 ddls,一个是航空公司,一个是酒店,第一个来自国家/地区的 ddl以From City为界,To Country和To city也是如此,之后Airlines的ddl以To City为界,hotel也是如此,所以最后当用户在From Country中选择一个国家时,他会得到From city中的城市,对于 To Country,他会得到 To city,当他从 City 中选择一个城市时,他会得到在 To City 中选择的城市的航空公司,这就是我重新创建 ddls 的原因
猜你喜欢
  • 2010-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-19
  • 2012-01-03
  • 1970-01-01
  • 1970-01-01
  • 2011-03-25
相关资源
最近更新 更多