[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 597
  • Last Modified:

Usercontrol event firing multiple times asp.net c#

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
0
codingitupagain
Asked:
codingitupagain
1 Solution
 
Mike EghtebasDatabase and Application DeveloperCommented:
For time being, you maybe want to do the following until someone has a better solution:
Add a Session["ClickIsDone"]="no"; in 

            if (IsPostBack == false)
            {
                Session["Counter"] = 0;
                Session["QuestionCount"] = 0;
                Session["CompletedInsp"] = false;
                Session["ClickIsDone"]="no";
            }

and use it at:

        protected void Button1_Click(object sender, EventArgs e)
        {
             if(Session["ClickIsDone"]=="no")      //<--  you may need to check if Session["ClickIsDone"]==null to set it to "no" here, if it is.
             {
                  Response.Redirect("WebForm1.aspx");
             }
             else
            {
                 Session["ClickIsDone"]="yes"
            }
        }

Open in new window

And then find a place to reset it to "no" again.

Mike
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now