【发布时间】: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_Init,Page_Load更好。 -
我已经使用了
if(!IsPostBack),但它不能正常工作,对于 Page_Load 也一样,什么都不能工作:/ -
一种避免使用回发的方法是使用 ajax。在您的 aspx 文件中创建一个 javascript 脚本来操作此下拉列表。
标签: c# asp.net drop-down-menu