标签不会出现在asp.net中

时间:2013-06-08 14:35:01

标签: asp.net label

我在asp.net页面中有一个标签,并在某些情况下更改其文本。这是代码:

<asp:Label ID="errorMessage" runat="server" Text="Label" Visible="False"></asp:Label>

 errorMessage.Text = MyGlobals.student.registerCourse(c, ref addList, course).ToString();
 errorMessage.ForeColor = System.Drawing.Color.Red;
 errorMessage.Visible = true;

当我调试时,我看到MyGlobals.student.registerCourse(c,ref addList,course).ToString()方法很好地返回了我的错误信息。然后我将标签设置为可见,但是当页面加载时我看不到标签。此外,在调试时,我看到标签的“textsetbyaddparsedsubobject”属性为false。这可能是问题吗?为什么我的页面中没有显示标签?有人可以帮忙吗?

感谢。

编辑:以下是完整代码:

 protected void bSubmitChanges_Click1(object sender, EventArgs e)
{
    Userfunctions function = new Userfunctions();
    List<string> dropList = new List<string>();
    List<string> addList = new List<string>();
    SqlConnection con = new SqlConnection();
    con.ConnectionString = Userfunctions.GetConnectionString();
    SqlCommand cmd;
    con.Open();
    string ID = MyGlobals.currentID;


    try
    {
        for (int i = 1; i <= 6; i++)
        {
            string course;
            if ((course = boxCRN(i)) != "")
            {
                cmd = new SqlCommand("select count (*) from CourseTable where CRN=@course", con);
                cmd.Parameters.AddWithValue("@course", course);
                int result = Convert.ToInt32(cmd.ExecuteScalar());

                if (result > 0)
                {
                    cmd = new SqlCommand("select * from CourseTable where CRN=@course", con);
                    cmd.Parameters.AddWithValue("@course", course);

                    cmd.ExecuteScalar();

                    SqlDataAdapter da = new SqlDataAdapter(cmd);

                    DataTable dt = new DataTable();
                    da.Fill(dt);

                    foreach (DataRow dr in dt.Rows)
                    {
                        string query = "SELECT * FROM CourseTable WHERE CourseCode='" + dr["CourseCode"] + "' AND CourseNumber='" + dr["CourseNumber"] + "' AND Term='" + dr["Term"] + "'";
                        cmd = new SqlCommand(query, con);
                        SqlDataAdapter da2 = new SqlDataAdapter(cmd);

                        DataTable dt2 = new DataTable();
                        da2.Fill(dt2);
                        DataRow dr2 = dt2.Rows[0];

                        cmd = new SqlCommand("select * from PrereqTable where CourseCode='" + dr["CourseCode"] + "' AND CourseNumber='" + dr["CourseNumber"] + "' AND Term='" + dr["Term"] + "'", con);

                        da2 = new SqlDataAdapter(cmd);

                        dt2 = new DataTable();
                        da2.Fill(dt2);
                        List<string> pre = new List<string>();
                        foreach (DataRow dr5 in dt2.Rows)
                        {
                            pre.Add(Convert.ToString(dr5["pCourseCode"]) + Convert.ToString(dr5["pCourseNumber"]));
                        }


                        Course c = new Course(dr2["InstructorID"].ToString(), dr2["CourseCode"].ToString(), dr2["CourseNumber"].ToString(), dr2["CourseName"].ToString(), dr2["Term"].ToString(), dr2["CRN"].ToString(), dr2["Level"].ToString(), dr2["Credit"].ToString(), dr2["Description"].ToString(), dr2["Capacity"].ToString());

                        c.addPrereq(pre);
                        string message = MyGlobals.student.registerCourse(c, ref addList, course).ToString();
                        errorMessage.Text = message;
                        errorMessage.ForeColor = System.Drawing.Color.Red;
                        errorMessage.Visible = true;
                        Label1.Visible = true;
                        Label1.Text = "asdasdasd";


                    }

                }

            }
        }
    }
    catch (Exception) { }

    for (int i = 0; i < showCourses.Rows.Count; i++)
    {
        string a = ((DropDownList)showCourses.Rows[i].FindControl("actionmenu")).SelectedValue;
        if (((DropDownList)showCourses.Rows[i].FindControl("actionmenu")).SelectedValue == "1")
        {
            string courseCode = showCourses.Rows[i].Cells[1].Text, courseNumber = showCourses.Rows[i].Cells[2].Text;

            SqlCommand com = new SqlCommand("select * from CourseTable where CourseCode=@courseCode and CourseNumber=@courseNumber", con);
            com.Parameters.AddWithValue("courseCode", courseCode);
            com.Parameters.AddWithValue("courseNumber", courseNumber);

            try
            {
                SqlDataAdapter da2 = new SqlDataAdapter(com);

                DataTable dt2 = new DataTable();
                da2.Fill(dt2);
                DataRow dr2 = dt2.Rows[0];

                Course c = new Course(dr2["InstructorID"].ToString(), dr2["CourseCode"].ToString(), dr2["CourseNumber"].ToString(), dr2["CourseName"].ToString(), dr2["Term"].ToString(), dr2["CRN"].ToString(), dr2["Level"].ToString(), dr2["Credit"].ToString(), dr2["Description"].ToString(), dr2["Capacity"].ToString());
                Register reg = new Register(c, MyGlobals.student);

                MyGlobals.student.dropCourse(reg);

                dropList.Add(showCourses.Rows[i].Cells[1].Text + showCourses.Rows[i].Cells[2].Text);
            }
            catch (Exception) { }
        }
    }

    foreach (string course in dropList)
    {
        for (int i = 0; i < MyGlobals.student.getRegistered().Count; i++ )
        {
            if (MyGlobals.student.getRegistered()[i].getCourse().getCode().ToString() + MyGlobals.student.getRegistered()[i].getCourse().getNumber().ToString() == course)
                MyGlobals.student.dropCourse(MyGlobals.student.getRegistered()[i]);
        }
        cmd = new SqlCommand("delete from RegisterTable where StudentID='" + MyGlobals.currentID + "' and CourseCode='" + course.Substring(0, course.Length - 3) + "' and CourseNumber='" + course.Substring(course.Length - 3, 3) + "'", con);
        cmd.ExecuteNonQuery();
    }

    try
    {
        foreach (string courses in addList)
        {
            string courseCode = "";
            string courseNumber = "";
            string term = MyGlobals.currentTerm + " " + MyGlobals.currentYear;
            string q = ("select CourseCode from CourseTable where CRN=@courses");
            string grade = "";
            SqlCommand command = new SqlCommand(q, con);
            command.Parameters.AddWithValue("@courses", courses);
            courseCode = Convert.ToString(command.ExecuteScalar());
            q = ("select CourseNumber from CourseTable where CRN=@courses");
            command = new SqlCommand(q, con);
            command.Parameters.AddWithValue("@courses", courses);
            courseNumber = Convert.ToString(command.ExecuteScalar());


            cmd = new SqlCommand("insert into RegisterTable (CourseCode,CourseNumber,Term,StudentID,Grade) values(@courseCode,@courseNumber,@term, @ID,'U')", con);
            cmd.Parameters.AddWithValue("@courseCode", courseCode);


            cmd.Parameters.AddWithValue("@courseNumber", courseNumber);

            cmd.Parameters.AddWithValue("@term", term);

            cmd.Parameters.AddWithValue("@ID", ID);
            cmd.Parameters.AddWithValue("@grade", grade);

            cmd.ExecuteNonQuery();
        }
    }
    catch (Exception) { }
    con.Close();

    Response.Redirect("AddDropClasses.aspx");

}
}

注册课程功​​能:

 public  string registerCourse(Course course, ref List <string> addList, string crn) {
    bool registered = true;
    string message ="";
    foreach (string s in course.getTime())
    {
        Userfunctions f = new Userfunctions();
        foreach (Register r in this.register) {
            if (r.getCourse().getTerm() == MyGlobals.currentTerm.ToString() + " " + MyGlobals.currentYear.ToString() && !f.TimeCheck(s, r.getCourse().getTime()))
            {
                registered = false;
                message = "Time conflict";
            }
        }
    }
    if (registered) {

        SqlConnection con = new SqlConnection();
        con.ConnectionString = Userfunctions.GetConnectionString();
        con.Open();
        string id = MyGlobals.currentID;
        SqlCommand cmd = new SqlCommand("SELECT count (*) from RegisterTable where CourseCode ='" + course.getCode() +"' and CourseNumber='" + course.getNumber() + "' and Term='" + course.getTerm()+"'" , con);
        cmd.Parameters.AddWithValue("@id", id);
        int active = Convert.ToInt32(cmd.ExecuteScalar());
        if (active >= Convert.ToInt32(course.getCapacity())){
            registered = false;
            message = "Not enough capacity";
        }
        if (registered) {
            foreach (string s in course.getPrerequisites()) {
                if (!hasPassedCourse(s)) {
                    registered = false;
                    message = "Prerequisite error";
                }
            }
        }

    }

    if (registered)
    {
        Register reg = new Register(course, MyGlobals.student);
        MyGlobals.student.addToSchedule(reg);
        addList.Add(crn);
    }

    return message;
}

2 个答案:

答案 0 :(得分:2)

删除以下行

Response.Redirect("AddDropClasses.aspx");

它会再次将您的页面加载为新页面

答案 1 :(得分:-1)

如果在控件上设置 Visible =“false”,则控件属性可能未完全实例化。

我会尝试在Page_Load的代码隐藏中使用 errorMessage.Visible = false 隐藏控件。