标签表现不正常

时间:2016-04-13 10:57:24

标签: c# asp.net label

我正在开发一个在线航空预订系统,我有两个下拉列表来选择来源和目的地以及一个标签。这个标签将显示"没有航班"如果没有从数据库中检索到匹配的路由(在这种情况下是它的sqlserver 2008).i已经编写了下面的代码试图这样做,但是当我回复或刷新页面标签时用"没有航班"直到可见。我的代码出了什么问题,请任何人帮助我。

public partial class Dropdndemo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       SqlConnection con=new SqlConnection("Data Source=KUNDAN-PC\\SQLEXPRESS;Initial Catalog=Ars2.1.2;Integrated Security=True");
        //string Sqlcmnd="select Source from Ars2.1.2.dbo.Scheduling";
       con.Open();
       if (!Page.IsPostBack)
       {
           SqlCommand com = new SqlCommand("select distinct Source from Schedulings", con);
           SqlCommand comn=new SqlCommand("select distinct Destination from Schedulings", con);
           //SqlDataReader readr;
           DropDownList1.DataSource = com.ExecuteReader();

           DropDownList1.DataTextField = "Source";
           // DropDownList1.DataTextField = "Destination";
           DropDownList1.DataBind();
           DropDownList1.Items.Insert(0, "select Source");
           con.Close();
           con.Open();


           DropDownList2.DataSource = comn.ExecuteReader();
           DropDownList2.DataTextField = "Destination";
           DropDownList2.DataBind();
           DropDownList2.Items.Insert(0, "select Destination");
           con.Close();


       }
        //con.Close();
       // DropDownList1.DataBind();
        //con.Close();
        if (IsPostBack)
           Label3.Text = "";
       //Label1.Visible = false;



    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
       // string Source = DropDownList1.SelectedValue.ToString();
       // Label1.Text = Source;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {


            string src = DropDownList1.SelectedItem.ToString();
            string desti = DropDownList2.SelectedItem.ToString();

            if ((src == desti) && IsPostBack)
            {


                Label1.Text = "Source And Destination cant be same!";


            }

            SqlConnection lop = new SqlConnection("Data Source=KUNDAN-PC\\SQLEXPRESS;Initial Catalog=Ars2.1.2;Integrated Security=True");

            lop.Open();
            SqlCommand cmd = new SqlCommand("select * from Schedulings where Source=@Source and Destination=@Destination", lop);
            cmd.Parameters.AddWithValue("Source", DropDownList1.SelectedItem.Text);
            cmd.Parameters.AddWithValue("Destination", DropDownList2.SelectedItem.Text);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count == 0)
            {
                Label3.Text = "No planes available in this route!!!";

            }



    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {


    }
}

1 个答案:

答案 0 :(得分:0)

我想您正在使用 F5 刷新页面,或者右键单击页面并选择刷新选项或使用浏览器的刷新按钮。如果是这种情况,那么我很害怕它没有刷新,它重复以前的行动。这意味着如果您使用上述选项搜索航班和刷新,它将再次使用相同的搜索条件搜索航班。您可以通过将调试器放在搜索事件上来确认它。您可以通过设置和清除Session或ViewState并使用它来管理标签文本来避免此行为。

相关问题