单击按钮时页面元素消失

时间:2019-03-22 17:25:27

标签: c# html asp.net

我遇到了页面上所有元素和内容消失的问题。

我正在使用C#和ASP.net

当我单击该按钮时,页面中动态添加的所有内容和元素都会消失,并且变得不可见。

哪个部分出了问题,如何解决该问题?

这是到目前为止所做的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Questions : System.Web.UI.Page
{
    public List<string> Ques = new List<string>();
    public List<string> Ans = new List<string>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Ques.Clear();
            Ans.Clear();
            get_questions();

            for (int i = 0; i < Ques.Count() && Ans[i] != "null"; i++)
            {
                Draw_answered_questions(Ques[i], Ans[i]);
            }

            for (int i = 0; i < Ques.Count(); i++)
            {
                if (Ans[i] == "null")
                    Draw_unanswered_questions(Ques[i]);
            }

            answered.Attributes.Add("class", "displayblock");
            unAnswered.Attributes.Add("class", "displaynone");
            answerArea.Attributes.Add("class", "displaynone");
        }
    }

    //to show questions that have answer.
    protected void Button2_Answered_Questions_Click(object sender, EventArgs e)
    {
        answered.Attributes.Add("class", "displayblock");
        unAnswered.Attributes.Add("class", "displaynone");
        answerArea.Attributes.Add("class", "displaynone");

        answered.InnerHtml = "";
        Ques.Clear();
        Ans.Clear();
        get_questions();

        for (int i = 0; i < Ques.Count() && Ans[i] != "null"; i++)
        {
            Draw_answered_questions(Ques[i], Ans[i]);
        }
    }

    //to show questions that haven't answer yet.
    protected void Button3_Questions_without_answer_Click(object sender, EventArgs e)
    {       unAnswered.Attributes.Add("class", "displayblock");
            answered.Attributes.Add("class", "displaynone");
            answerArea.Attributes.Add("class", "displaynone");

            unAnswered.InnerHtml = "";
            Ques.Clear();
            Ans.Clear();
            get_questions();

            for (int i = 0; i < Ques.Count(); i++)
            {
                if (Ans[i] == "null")
                    Draw_unanswered_questions(Ques[i]);
            }

    }

    //add new question.
    protected void Button1_Ask_Click(object sender, EventArgs e)
    {
        New_Question(ques_textBox.Text, "null" , 2);
        Draw_unanswered_questions(ques_textBox.Text);

    }

    //get data from database.
    protected void get_questions()
    {
        using (ScrummanagementEntities db = new ScrummanagementEntities())
        {
            var query = from q in db.Questions select q;
            List<Question> copies = query.ToList();
            Ques.Clear();
            Ans.Clear();

            for(int i=0 ; i<copies.Count() ; i++)
            {
                Ques.Add(copies[i].Question1);
                Ans.Add(copies[i].Answer);
            }
        }
    }

    //draw new divs to all answered questions
    protected void Draw_answered_questions(string q , string a)
    {
        HtmlGenericControl newdiv1 = new HtmlGenericControl("div");
        newdiv1.Attributes.Add("class", "question");
        newdiv1.InnerHtml = q;
        answered.Controls.Add(newdiv1);

        HtmlGenericControl newdiv2 = new HtmlGenericControl("div");
        newdiv2.Attributes.Add("class", "answer");
        newdiv2.InnerHtml = a;
        answered.Controls.Add(newdiv2);
    }

    //draw new divs to all unanswered questions
    protected void Draw_unanswered_questions(string q)
    {
        //HtmlGenericControl newdiv1 = new HtmlGenericControl("div");
        //newdiv1.Attributes.Add("class", "unans");
        //newdiv1.Attributes.Add("id", q);
        //newdiv1.InnerHtml = q;
        //newdiv1.Attributes.Add("OnClick", "unans_Click");
        //unAnswered.Controls.Add(newdiv1);

        int index = 0;
        Button b = new Button();
        b.Attributes.Add("class", "unans");
        index++;
        b.Text = q;
        b.Attributes.Add("runat", "server");
        unAnswered.Controls.Add(b);
        b.Click += new EventHandler(unans_Click); //problem here

        //b.Attributes.Add("id", q+index.ToString());
        //b.Attributes.Add("OnClick", "unans_Click");
        //b.Attributes.Add("onClick", "unans_Click");
    }

    private void unans_Click(object sender, EventArgs e)
    {
        HtmlGenericControl b = sender as HtmlGenericControl;
        b.InnerHtml = "fffff";

        //HtmlGenericControl newdiv1 = sender as HtmlGenericControl;
        //answerArea.Attributes.Add("class", "displayblock");
        //answered.Attributes.Add("class", "displaynone");
        //unAnswered.Attributes.Add("class", "displaynone");
    }

    //get all data from database
    public void New_Question(string ques, string ans, int proid)
    {
        using (ScrummanagementEntities db = new ScrummanagementEntities())
        {
            Question q = new Question();
            q.Question1 = ques;
            q.Answer = ans;

            db.Questions.Add(q);
            db.SaveChanges();
        }
    }
    protected void Button_the_Answer_Click(object sender, EventArgs e)
    {

    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Button b = sender as Button;
        b.Text = "fffff";
    }
}

0 个答案:

没有答案