当前位置:编程学习 > C#/ASP.NET >>

为什么我在执行 if。。else的时候 重新执行了两遍?

能帮我看看原因吗?
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using WSE_Booking.BLL;
using WSE_Booking.Model;

public partial class M_预约课程5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["M_Uname"] == null)
        { Response.Redirect("WSE_Login.aspx"); }
        else
        {
            Label1.Text = "";
 SqlDataSource1.SelectCommand = "select * from 课程信息表 where C_Time>'" + DateTime.Now + "' and C_ID in(select C_ID from(select C_ID,COUNT(C_ID) as Times from 定课表 group by C_ID having COUNT(C_ID)<C_Num)t) or C_Time>'" + DateTime.Now + "' and C_ID not in(select C_ID from 定课表) order by C_ID desc";
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DateTime a = DateTime.Now;
        if (DropDownList1.SelectedItem.Value.ToString() == "Encounter")
            SqlDataSource1.SelectCommand = "select * from 课程信息表 where C_Time>'" + a + "' and C_Style='Encounter' and C_ID in(select C_ID from(select C_ID,COUNT(C_ID) as Times from 定课表 group by C_ID having COUNT(C_ID)<4)t) or C_Style='Encounter' and C_Time>'" + DateTime.Now + "' and C_ID not in(select C_ID from 定课表) order by C_ID desc";
        else if (DropDownList1.SelectedItem.Value.ToString() == "Complementary Class")
            SqlDataSource1.SelectCommand = "select * from 课程信息表 where C_Time>'" + a + "' and C_Style='Complementary Class' and C_ID in(select C_ID from(select C_ID,COUNT(C_ID) as Times from 定课表 group by C_ID having COUNT(C_ID)<8)t) or C_Style='Complementary Class' and C_Time>'" + DateTime.Now + "' and C_ID not in(select C_ID from 定课表) order by C_ID desc";
        else if (DropDownList1.SelectedItem.Value.ToString() == "Social Club")
            SqlDataSource1.SelectCommand = "select * from 课程信息表 where C_Time>'" + a + "' and C_Style='Social Club' and C_ID in(select C_ID from(select C_ID,COUNT(C_ID) as Times from 定课表 group by C_ID having COUNT(C_ID)<12)t) or C_Style='Social Club' and C_Time>'" + DateTime.Now + "' and C_ID not in(select C_ID from 定课表)  order by C_ID desc";
        else
            SqlDataSource1.SelectCommand = "select * from 课程信息表 where C_Time>'" + a + "' and C_ID in(select C_ID from(select C_ID,COUNT(C_ID) as Times from 定课表 group by C_ID having COUNT(C_ID)<C_Num)t) or C_Time>'" + DateTime.Now + "' and C_ID not in(select C_ID from 定课表) order by C_ID desc";
        GridView1.DataBind();

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < TextBox3.Text.Length; i++)//是否录入的编号为数字
        {
            if (char.IsNumber(TextBox3.Text[i]) != true)
            {
                Label1.Text = "您输入的编号错误,请重新输入!! ";

            }
            else
            {
                Label1.Text = " ";
         
        if (TextBox3.Text != "" && TextBox4.Text != "" && new 课程信息Bll().Exists2(Convert.ToInt32(TextBox3.Text)) == true)//用户名,编号不能为空,且该课程编号要存在
        {
            定课表 a = new 定课表();
            a.C_ID = Convert.ToInt32(TextBox3.Text);
            a.S_Uname = TextBox4.Text;
            string strCon = "Data Source=53E2ED31D85A42C\\SQLEXPRESS;Initial Catalog=wall steet English Booking System;Integrated Security=True";
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = strCon;
            cn.Open();

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT C_Style FROM 课程信息表 WHERE C_ID='" + TextBox3.Text + "'";
            cmd.Connection = cn;

            a.C_Style = Convert.ToString(cmd.ExecuteScalar());


            SqlCommand cmd1 = new SqlCommand();
            cmd.CommandText = "SELECT C_Time FROM 课程信息表 WHERE C_ID='" + TextBox3.Text + "'";
            cmd.Connection = cn;

            a.C_Time = Convert.ToDateTime(cmd.ExecuteScalar());

            SqlCommand cmd2 = new SqlCommand();
            cmd.CommandText = "SELECT T_Name FROM 课程信息表 WHERE C_ID='" + TextBox3.Text + "'";
            cmd.Connection = cn;

            a.T_Name = Convert.ToString(cmd.ExecuteScalar());
            SqlCommand cmd5 = new SqlCommand();

            cmd.CommandText = "SELECT C_Hours FROM 课程信息表 WHERE C_ID='" + TextBox3.Text + "'";
            cmd.Connection = cn;

            a.C_Hours = Convert.ToString(cmd.ExecuteScalar());


            cn.Close();
            if (new 学员信息Bll().Exists(TextBox4.Text) == true)//判断是否存在该用户
            {

                if (new 定课信息Bll().Exists(Convert.ToInt32(TextBox3.Text)) == false)//验证课程编号在定课表中是否存在,如果不出在课直接添加成功!
                {
                    new 定课信息Bll().Add(a);

                    Label1.Text = "课程预约成功!!!";

                }
                
                else if (new 定课信息Bll().Exists2(TextBox4.Text) == true)//判断是否存在该学员同一个课程的信息,如果不存在,预约成功!
                {
                    string strCon2 = "Data Source=53E2ED31D85A42C\\SQLEXPRESS;Initial Catalog=wall steet English Booking System;Integrated Security=True";
                    SqlConnection cn2 = new SqlConnection();
                    cn2.ConnectionString = strCon2;
                    cn2.Open();

                    SqlCommand cmd4 = new SqlCommand();
                    cmd4.CommandText = "SELECT C_ID FROM 定课表 WHERE S_Uname='" + TextBox4.Text + "' AND C_ID='" + TextBox3.Text + "'";
                    cmd4.Connection = cn2;
                    int c;
                    c = Convert.ToInt32(cmd4.ExecuteScalar());

                    if (c == 0)
                    {
                        new 定课信息Bll().Add(a);

                        Label1.Text = "课程预约成功!!!";

                    }
                    else
                    {

                        Label1.Text = "定课失败,该学员已经预约过该课程,无法重复预约!";
                    }
                    cn2.Close();
                }
                else if (new 定课信息Bll().Exists(Convert.ToInt32(TextBox3.Text)) == true)//判断是否存在该学员同一个课程的信息,如果不存在,预约成功!
                {
                    string strCon3 = "Data Source=53E2ED31D85A42C\\SQLEXPRESS;Initial Catalog=wall steet English Booking System;Integrated Security=True";
                    SqlConnection cn3 = new SqlConnection();
                    cn3.ConnectionString = strCon3;
                    cn3.Open();

                    SqlCommand cmd3 = new SqlCommand();
                    cmd3.CommandText = "SELECT S_Uname FROM 定课表 WHERE S_Uname='" + TextBox4.Text + "' AND C_ID='" + TextBox3.Text + "'";
                    cmd3.Connection = cn3;
                    string d;
                    d = Convert.ToString(cmd3.ExecuteScalar());
                    cn3.Close();
                    if (d == null || d.Equals(""))
                    {
                        new 定课信息Bll().Add(a);

                        Label1.Text = "课程预约成功!!!";
                    }
                    else
                    {
                        Label1.Text = "定课失败,该学员已经预约过该课程,无法重复预约!";
                    }
                }

            }
            else
            {
                Label1.Text = "不存在该用户,请重新输入!!!";

            }

        }
        else
        {
            Label1.Text = "您没有输入课程编号或用户名或录入的信息有误,请重新录入!!!";
        }

      }
    }
    }
}



我在录入信息后,点击 添加后,该程序从if开始执行,执行后,又重新从if开始执行了一遍,这是为什么?能帮我解决下吗? --------------------编程问答--------------------  1.调试
 2.检查代码吧,执行顺序  if (DropDownList1.SelectedItem.Value.ToString() == "Encounter")
  明显的{}都没有
--------------------编程问答-------------------- Page_Load重复执行吗?

 if (!IsPostBack)
{
   if ... else ...
} --------------------编程问答-------------------- 调试看下,

是不是刷新又执行一遍了、 --------------------编程问答-------------------- 我就是在想是不是刷新又刷新的问题?可是每次都这样 --------------------编程问答-------------------- if(!ispostback)
{
 
} --------------------编程问答-------------------- 同上 ispostback  --------------------编程问答-------------------- if(!IsPostBack) --------------------编程问答--------------------   if (!Page.IsPostBack)
        {

少了这个 --------------------编程问答-------------------- 楼主,我猜你是在2个浏览器请求了你的页面,相当于2个线程,所以代码执行2次,我猜的 --------------------编程问答-------------------- for (int i = 0; i < TextBox3.Text.Length; i++)//是否录入的编号为数字
  {
  if (char.IsNumber(TextBox3.Text[i]) != true)
  {
  Label1.Text = "您输入的编号错误,请重新输入!! ";

  }}
我解决了,是应为 for (int i = 0; i < TextBox3.Text.Length; i++)的问题,我要是输入两位数它就自动执行两遍。。。。。 我错了。。。对不起大家 --------------------编程问答-------------------- 哪就散分 --------------------编程问答-------------------- if(!ispostback)
{


这个不能少、   --------------------编程问答-------------------- if (!IsPostBack){}吧你的if放到这个里面 --------------------编程问答--------------------  protected void Page_Load(object sender, EventArgs e)
  {
     if (!IsPostBack){//用于判断是提交还是刷新
              .....................添加您自己代码
}
} --------------------编程问答-------------------- if (!IsPostBack)
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,