We help IT Professionals succeed at work.
Get Started

Usercontrol event firing multiple times asp.net c#

798 Views
Last Modified: 2014-08-13
Hi,

I've got the following code: -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Testing.Event_Delegate
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        WebUserControl2 QT2;
        WebUserControl3 QT3;
        DataTable dtQuestionSet;

        protected void Page_Init(object sender, EventArgs e)
        {
            dtQuestionSet = (DataTable)Session["DataTableW"];
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                Session["Counter"] = 0;
                Session["QuestionCount"] = 0;
                Session["CompletedInsp"] = false;
            }

            //check to see if the inspection process is complete and display pass\fail and log. Else carry on.
            if (Convert.ToBoolean(Session["CompletedInsp"]) == true)
            {
                //Response.Write("Failed");
                ModalPopupExtender2.Show();
                return;
            }

            // all controls need to be rendered every page postback otherwise the rest of the code loses scope.
            QT2 = (WebUserControl2)this.LoadControl("WebUserControl2.ascx");
            UC2BtnClickEvent.MyEvent2 += new UC2BtnClickEvent.OnMyEvent2(ChecKResult);
            QT3 = (WebUserControl3)this.LoadControl("WebUserControl3.ascx");
            UC2BtnClickEvent.MyEvent3 += new UC2BtnClickEvent.OnMyEvent3(ChecKResult);
            PlaceHolder2.Controls.Add(QT2);
            PlaceHolder2.Controls.Add(QT3);

            QT2.Visible = false;
            QT3.Visible = false;

            if (IsPostBack == false)
            {
                dtQuestionSet = new DataTable();
                MakeDataTabledtQuestionSet();
            }
            else
            {
                dtQuestionSet = (DataTable)Session["DataTableW"];
            }
            Session["DataTableW"] = dtQuestionSet;
        }

        void ChecKResult(object sender, EventArgs e, string strTextBox, string strMyDescription)
        {
            Label1.Text = "You Answered: - " + strMyDescription;

            Session["Counter"] = Convert.ToInt32(Session["Counter"]) + 1;

            if (Convert.ToInt32(Session["Counter"]) > Convert.ToInt32(Session["QuestionCount"]))
            {
                Session["CompletedInsp"] = true;
            }
            else
            {
                //carry on with inspection
                dtQuestionSet = (DataTable)Session["DataTableW"];
                RunInspection();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("WebForm1.aspx");
        }

        protected void cmdStartInspection_Click(object sender, EventArgs e)
        {
            Session["Counter"] = 0;
            GetQuestions();
        }

        protected void GetQuestions()
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["PromptedInspConnectionString"].ConnectionString);
            conn.Open();

            SqlCommand cmd = new SqlCommand("test.GetQuestions", conn);
            SqlDataReader dr = null;
            cmd.CommandType = CommandType.StoredProcedure;
            //cmd.Parameters.Add(new SqlParameter("@Block1", cmbBlock1.Text));

            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                DataRow drQuestions = dtQuestionSet.NewRow();
                drQuestions["QuestionText"] = dr[1].ToString();
                drQuestions["ExpectedResponse"] = dr[2].ToString();
                drQuestions["QuestionType"] = dr[3].ToString();
                dtQuestionSet.Rows.Add(drQuestions);
            }
            Session["DataTableW"] = dtQuestionSet;
            Session["QuestionCount"] = dtQuestionSet.Rows.Count;

            conn.Close();

            RunInspection();
        }

        protected void RunInspection()
        {
            switch (dtQuestionSet.Rows[Convert.ToInt32(Session["counter"])][2].ToString())
            {
                case "1":
                    QT2.QuestionText = dtQuestionSet.Rows[0][0].ToString();//"I'm asking a question 1";
                    QT2.Visible = true;
                    break;
                case "2":

                    QT3.QuestionText = dtQuestionSet.Rows[0][0].ToString();//"I'm asking a question 2";
                    QT3.Visible = true;
                    break;
            }
        }

        private void MakeDataTabledtQuestionSet()
        {
            dtQuestionSet.Columns.Add("QuestionText");
            dtQuestionSet.Columns.Add("ExpectedResponse");
            dtQuestionSet.Columns.Add("QuestionType");

        }

    }
}

Open in new window


When I click on the button on a usercontrol with the code: -

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

namespace Testing.Event_Delegate
{
    public partial class WebUserControl2 : System.Web.UI.UserControl
    {
        string questionAsked;

        public string QuestionText
        {
            set { questionAsked = value; }
            get { return questionAsked; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = QuestionText;
            TextBox1.Focus();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            UC2BtnClickEvent bEvent = new UC2BtnClickEvent();
            bEvent.onRefresh2Event3(sender, e, TextBox1.Text, TextBox1.Text );
            TextBox1.Text = "";
        }
    }
}

Open in new window


it runs the procedure "CheckResults" 3 times when I only need it to run once.

what have I done wrong to make this happen please as it should only run once.

Many Thanks
Lee
Comment
Watch Question
Database and Application Developer
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE