DropDownLists Issue ASP.NET

大兔子大兔子 提交于 2020-01-15 10:26:43

问题


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 wich is the countries, and the third dropdownlist is the airlines who depends on the selected value of the second dropdownlist wich is the cities.

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 im frustrated about this issue

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();
        }
    }
}

Thanks


回答1:


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:

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

Check this and let me know.




回答2:


Your problem is permanently recreate ddl.

        gather_Tocountries();
        gather_Tocities();

Well, for solution this problem, you need after created your ddls set selected value for ToCit and 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. First init you can do in pageInit or pageLoad but with use 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

then

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


来源:https://stackoverflow.com/questions/59306876/dropdownlists-issue-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!