在哪里我可以把会话除了页面加载asp.net与c#

时间:2016-03-22 09:42:41

标签: c# asp.net session

我创建了一个网页,其中通过会话从以前的登录页面获取3个数据库值。基本上当前页面用于预订公交车座位。其中手机号码是必填字段。如果未输入手机号码,则无法预订座位。通过会话我从表中得到3个字段,其中一个是移动电话号码。如果手机号码已经在表格中,则无需输入手机号码。但如果没有,您必须输入手机号码才能预订座位,因为它是必填字段。现在,问题是当我试图预订座位时我无法预订,尽管我输入了手机号码。总是以“请输入手机号码以便成功预订...... !!!”

请参考以下代码: -

protected void Page_Load(object sender, EventArgs e)
{

    Label1.Text = Session["PERNR"].ToString();
    Label2.Text = Session["ZZFNAME"].ToString();
    TextBox1.Text = Session["MOBILE"].ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
    if (DropDownList1.Text == "Dahej to Baroda")
    {
        DateTime dt = Convert.ToDateTime(TextBox6.Text);
        string day = dt.DayOfWeek.ToString();
        if (dt >= DateTime.Now)
        {

            if (day == "Saturday")
            {
                DataSet ds = new DataSet();
                OracleConnection con = new OracleConnection("Data Source=10.127.240.216/ipcldb;User ID=ipcltos;Password=ipcltos;Unicode=True");
                con.Open();
                OracleDataAdapter a = new OracleDataAdapter("SELECT * FROM BARODA_BUS_BOOKING_DETAIL WHERE EMPCODE = '" + Label1.Text + "' AND JOURNEY_DATE='" + dt.ToString("dd-MMMM-yyyy") + "'", con);
                a.Fill(ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    string display = "Already Booked...!!!";
                    ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true);
                }
                else
                {

                    if(TextBox1.Text=="")
                    {
                        string display = "Please enter mobile number for successful booking...!!!";
                        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true);
                    }
                    else
                    {
                        try
                        {
                            OracleCommand cmd = new OracleCommand("INSERT INTO BARODA_BUS_BOOKING_DETAIL (EMPCODE,NAME,MOBILE,JOURNEY_TYPE,JOURNEY_DATE) VALUES('" + Label1.Text + "', '" + Label2.Text + "', '" + TextBox1.Text + "', '" + DropDownList1.Text + "','" + dt.ToString("dd-MMMM-yyyy") + "')", con);
                            cmd.ExecuteNonQuery();
                            string display = "Booked...!!!";
                            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true);
                            OracleCommand cmd1 = new OracleCommand("UPDATE SAP_EMPMST SET MOBILE = '" + TextBox1.Text + "' WHERE PERNR = '" + Label1.Text + "'", con);
                            cmd1.ExecuteNonQuery();

                            con.Close();

                        }
                        catch(Exception ex)
                        {
                            string display = "Invalid Number...!!!";
                            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true);
                        }
                    }
                }
            }
            else
            {
                string display = "Dahej to Baroda bus service available on Saturday only...!!!";
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true);

            }
        }
        else
        {
            string display = "You cannot book for the previous date...!!!";
            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

尝试将所有内容保留在!Ispostback 中,因为在回发时, TextBox1.Text 的值将会丢失

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
           Label1.Text = Session["PERNR"].ToString();
           Label2.Text = Session["ZZFNAME"].ToString();
           TextBox1.Text = Session["MOBILE"].ToString();
    }
}

答案 1 :(得分:0)

protected void Page_Load(object sender, EventArgs e)
{

    Label1.Text = Session["PERNR"].ToString();
    Label2.Text = Session["ZZFNAME"].ToString();
    if (!IsPostBack)
    {
        TextBox1.Text = Session["MOBILE"].ToString();
    }
}