在一页中使用两个面板

时间:2018-09-01 20:36:26

标签: c# asp.net

我为我的最后一个项目一直在做一个简单的网站,我想创建一个使用Winforms的预订系统。我想通过插入客户选择的座位数进行预订。客户在输入目的地和日期时将看到第一个面板,然后单击ibnext选择座位,这将引导第二个面板,客户必须输入姓名和电话。但就我而言,第二个面板永远不会被解雇,我意识到第二个面板永远不会成真时,我调试了一行。我没什么错请,我需要帮助。

public partial class _Default : System.Web.UI.Page
{
    SqlCommand cmd;
    SqlDataReader rea;
    SqlDataReader read;


    protected void Page_Load(object sender, EventArgs e)
    {
        seattable.Visible = false;
        ibnext.Visible = false;
        txtname.Focus();
    }


    protected void departon_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (departon.SelectedItem.Text.ToString() == "--Select--")
        {
            DepartureTime.Items.Clear();
        }
        else
        {

            DepartureTime.Items.Clear();
            string connString = ConfigurationManager.ConnectionStrings["MyDatabase1"].ConnectionString;
            SqlConnection conn = new SqlConnection(connString);

            conn.Open();

            string strsql = "Select * from TimeTable where Station_name = '" + Destination.SelectedItem.Text.ToString() + "'";
            SqlCommand cmd = new SqlCommand(strsql, conn);
            SqlDataReader rea;

            System.DateTime jtime = new DateTime();

            cmd.CommandText = strsql;
            rea = cmd.ExecuteReader();
            DepartureTime.Items.Add("--Select--");
            while (rea.Read())
            {
                jtime = System.DateTime.Parse(rea["Time"].ToString());
                if (departon.SelectedItem.Text.ToString() == System.DateTime.Today.ToShortDateString())
                {
                    if (jtime > System.DateTime.Now)
                    {
                        DepartureTime.Items.Add(rea["Time"].ToString());

                    }
                }
                else
                {
                    DepartureTime.Items.Add(rea["Time"].ToString());
                }
            }
            conn.Close();
        }
    }
    protected void Destination_SelectedIndexChanged(object sender, EventArgs e)
    {

        departon.Items.Clear();
        departon.Items.Add("--Select--");
        departon.Items.Add(System.DateTime.Today.ToShortDateString());
        departon.Items.Add(System.DateTime.Now.AddDays(1.00).ToShortDateString());
        departon.Items.Add(System.DateTime.Now.AddDays(2.00).ToShortDateString());
        departon.Items.Add(System.DateTime.Now.AddDays(3.00).ToShortDateString());
        departon.Items.Add(System.DateTime.Now.AddDays(4.00).ToShortDateString());
        departon.Items.Add(System.DateTime.Now.AddDays(5.00).ToShortDateString());

    }

    protected void DepartureTime_SelectedIndexChanged(object sender, EventArgs e)
    {

        string connecString = ConfigurationManager.ConnectionStrings["MyDatabase1"].ConnectionString;
    SqlConnection con = new SqlConnection(connecString);
        if (DepartureTime.SelectedItem.Text.ToString() != "--Select--")
        {
            seattable.Visible = true;
            DepartureTime.Enabled = false;

            departon.Enabled = false;
            Destination.Enabled = false;
            Source.Enabled = false;
            // display Rent
            con.Open();
            string strsq;
            strsq="Select * from TimeTable where Station_name = '" + Destination.SelectedItem.Text.ToString() + "'";

            SqlCommand cmmd = new SqlCommand(strsq, con);
            cmmd.CommandText = strsq;
            rea = cmmd.ExecuteReader();
            if (rea.HasRows == true)
            {
                while (rea.Read())
                {
                    litrent.Text = rea[2].ToString();
                    litrent1.Text = rea[2].ToString();
                    litbusnumber.Text = rea[5].ToString();
                }
            }

        }  rea.Close();
        con.Close();

        string strsqle;
        SqlDataReader rear;
        con.Open();
        strsqle= "Select * from States where ((date = '" + departon.SelectedItem.Text.ToString() + "') and (Time ='" + DepartureTime.SelectedItem.Text.ToString() + "')and (Station='" + Destination.SelectedItem.Text.ToString() + "'))";

        SqlCommand cmo= new SqlCommand(strsqle,con);

     // cmo.CommandText = strsqle;
        rear = cmo.ExecuteReader();
        while (rear.Read())
        {
            for (int j = 1; j <= 31; j++)
            {
                string s = "s" + j;
                if (rear[s.ToString()].ToString() == "b")
                {
                    ImageButton img = (ImageButton)pnl1.FindControl(s.ToString());
                    img.ImageUrl = "~/img/BoookedSeat.png";
                    img.Enabled = false;            
                }

            }
        }

    }

protected void ibnext_Click(object sender, ImageClickEventArgs e)
    {
        string[] str = txtseatnumber.Text.Split('s');
        int len = str.Length - 1;
        littotalseat.Text = len.ToString();
        int z = len * int.Parse(litrent.Text.ToString());
        littotalrent.Text = z.ToString();

        Random rnd = new Random();
        Random rnd1 = new Random();
        ///litpnr.Text = rnd.Next(50000).ToString(); to generate unique Ticket numbers 
        litpnr.Text = rnd.Next(50000).ToString() + rnd1.Next(548775).ToString();
        if (len == 0)
        {

            //  Label1.Text = "if";
            pnl1.Visible = true;
            pnl2.Visible = false;
            btnSave.Visible = false;
            Response.Redirect("Selectseat.aspx");

        }
        else
        {
            btnSave.Visible = true;
            pnl1.Visible = false;
            pnl2.Visible = true;
            ibnext.Visible = false;
            // Label1.Text = "else";
        }


    }
    protected void seatClick(ImageButton btn)
    {
        ibnext.Visible = true;
        if (btn.ImageUrl == "~/img/SelectedSeat.png")
        {
            seattable.Visible = true;
            btn.ImageUrl = "~/img/AvailableSeat.png";
            if (txtseatnumber.Text.IndexOf(btn.ID.ToString() + ",") > -1)
            {
                txtseatnumber.Text = txtseatnumber.Text.Replace(btn.ID.ToString() + ",", "");
            }
            else
            {
                txtseatnumber.Text = txtseatnumber.Text.Replace(btn.ID.ToString(), "");
            }
        }
        else if (btn.ImageUrl == "~/img/AvailableSeat.png")
        {
            seattable.Visible = true;
            btn.ImageUrl = "~/img/SelectedSeat.png";
            if (txtseatnumber.Text.Trim().Length == 0)
            {
                txtseatnumber.Text = btn.ID.ToString();
            }
            else if (txtseatnumber.Text.Trim().EndsWith(","))
            {
                txtseatnumber.Text = txtseatnumber.Text + btn.ID.ToString();
            }
            else
            {
                txtseatnumber.Text = txtseatnumber.Text + "," + btn.ID.ToString();
            }
        }


    }
    protected void s1click(object sender, ImageClickEventArgs e)
    {
        seatClick(s1);
    }
    protected void s2click(object sender, ImageClickEventArgs e)
    {
        seatClick(s2);
    }
    protected void s6click(object sender, ImageClickEventArgs e)
    {
        seatClick(s6);
    }
    protected void s7click(object sender, ImageClickEventArgs e)
    {
        seatClick(s7);
    }
    // THE REST OF THE S METHODS ARE CUTT IT BCZ OF THE WORD COUNT
    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {


        string connString = ConfigurationManager.ConnectionStrings["MyDatabase1"].ConnectionString;
         SqlConnection conbal = new SqlConnection(connString);
        conbal.Open();
        string strsqlbal = "select * from Users where (Username='" + User.Identity.Name.ToString() + "')";
        SqlCommand cmdbal = new SqlCommand(strsqlbal, conbal);

        cmdbal.Connection = conbal;
        cmdbal.CommandText = strsqlbal;

            string[] forsave = txtseatnumber.Text.Split(',');
            string strsql1= "select Count(*) from States where((Date='" + departon.SelectedItem.Text
                     + "') and (Time= '" + DepartureTime.SelectedItem.Text
                     + "') and (Station= '" + Destination.SelectedItem.Text
                     + "'))";
        object obj = cmdbal.ExecuteScalar();

        if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
        {
                    string sqladd = "INSERT into States ([Date],[Time],Station) values('" + departon.SelectedItem.Text
                          + "','" + DepartureTime.SelectedItem.Text
                          + "','" + Destination.SelectedItem.Text + "')";
            SqlCommand cmd1= new SqlCommand(sqladd, conbal);

            cmd1.ExecuteNonQuery();
            //ExecuteNonQuery(sqladd);

                }


            for (int j = 0; j <= forsave.Length - 1; j++)
            {
                if (forsave[j].ToString().Trim().Length > 0)
                {
                    string strsql2= "Update States set " + forsave[j].ToString() + " = 'b'  where ((Date='" + departon.SelectedItem.Text.ToString()
                       + "')and(Time='" + DepartureTime.Text.ToString()
                       + "')and (Station= '" + Destination.SelectedItem.Text.ToString()
                       + "'))";
                SqlCommand cmd2 = new SqlCommand(strsql2, conbal);
             // cmdbal.CommandText = "<New SQL Command>";
                cmd2.ExecuteNonQuery();

                }
            }






       string strsql3= "Insert Into passengerinfo values('" + litpnr.Text.ToString() + "','" + txtname.Text.ToString() + "'," + txtphone.Text.ToString() + ",'" + Destination.SelectedItem.Text.ToString() + "','" + Source.SelectedItem.Text.ToString() + "','" + departon.SelectedItem.Text.ToString() + "','" + DepartureTime.SelectedItem.Text.ToString() + "','" + littotalseat.Text.ToString() + "','" + txtseatnumber.Text.ToString() + "','" + littotalrent.Text.ToString() + "','" + User.Identity.Name.ToString() + "','Booked')";
        SqlCommand cmd3= new SqlCommand(strsql3,conbal);
        cmd3.ExecuteNonQuery();

            Response.Cookies["destination"].Value = Destination.SelectedItem.Text.ToString();
            Response.Cookies["pnr"].Value = litpnr.Text.ToString();
            Response.Cookies["name"].Value = txtname.Text.ToString();
            Response.Cookies["ph"].Value = txtphone.Text.ToString();
            Response.Cookies["jodate"].Value = departon.SelectedItem.Text.ToString();
            Response.Cookies["jotime"].Value = DepartureTime.SelectedItem.Text.ToString();
            Response.Cookies["seatnum"].Value = txtseatnumber.Text.ToString();
            Response.Cookies["totalseat"].Value = littotalseat.Text.ToString();
            Response.Cookies["rent"].Value = littotalrent.Text.ToString();
            Response.Cookies["busnumber"].Value = litbusnumber.Text.ToString();
            Response.Redirect("print1.aspx");

        }
    //    else
    //    {
    //        lblerror.Text = "Unsufficient Balance to book ticket.";
    //        //}

    //    }
    //}


}

1 个答案:

答案 0 :(得分:1)

将面板放入<asp:UpdatePanel>中,这意味着当您更改可见性时,它们应该显示和隐藏。您的代码应如下所示:

<asp:UpdatePanel runat="server" Id="upPanels" updateMode="conditional">
    <ContentTemplate>
        <panel Id="pnl1" runat="server">
            <!-- contents -->
         </panel>
         <panel Id="pnl2" runat="server">
            <!-- contents -->
         </panel>
    </ContentTemplate>
</asp:UpdatePanel>

在您的代码中,您需要调用upPanels.Update();来更新面板:

    if (len == 0)
    {

        //  Label1.Text = "if";
        pnl1.Visible = true;
        pnl2.Visible = false;
        btnSave.Visible = false;
    }
    else
    {
        btnSave.Visible = true;
        pnl1.Visible = false;
        pnl2.Visible = true;
        ibnext.Visible = false;
        // Label1.Text = "else";
    }
    upPanels.Update(); //update view

此外,您应该在SQL查询中为用户输入使用参数,这将防止SQL注入。 SqlParameter documentation here