DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: button event execute first before refreshing page

  1. #1
    Join Date
    Apr 2008
    Posts
    3

    button event execute first before refreshing page

    i am making a online survey that has dynamic questions. im having problem with my button event. it seems that my page is refreshed whenever onClick event is raised even it has to yet perform the codes in the said event. how can i make a way that the onClick event be executed and finished first before refreshing the page.

    example:

    protected void Button_Next_Click(object sender, EventArgs e){
    //i must finish first executing this method before refreshing the page




    //after i finish then i can now send the refreshed page
    }

  2. #2
    Join Date
    Jul 2007
    Location
    Minnesota
    Posts
    155
    can you post your code sample here?
    It sounds to me like you are putting the refresh code at the wrong location.

  3. #3
    Join Date
    Apr 2008
    Posts
    3

    my code...

    protected void Page_PreRender(object sender, EventArgs e)
    {

    ViewState["TheCurrentCategory"] = current_cat;
    ViewState["TheCurrentQuestion"] = current_quest_code.ToString();
    ViewState["TheUser"] = user;
    ViewState["renderLastPage"] = renderLastPage.ToString();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    if (this.IsPostBack)
    {
    clearUserInputControls();
    current_cat = (string)ViewState["TheCurrentCategory"];
    current_quest_code = Convert.ToInt32(ViewState["TheCurrentQuestion"]);
    user = (UserInstance)ViewState["TheUser"];
    renderLastPage = Convert.ToBoolean(ViewState["renderLastPage"]);
    if (!renderLastPage)
    Server.Transfer("Comment.aspx", false);
    }
    else
    {
    string username;
    if (Request.Cookies["userinfo"] != null)
    {
    renderLastPage = true;
    if (Request.Cookies["userinfo"]["username"] != null)
    {
    username = Request.Cookies["userinfo"]["username"];
    user = new UserInstance(username, qsetcode);
    }
    }
    else
    Response.Redirect("Error.aspx", true);
    }

    Category = new Dictionary<string, Dictionary<int, Dictionary<string, string>>>();
    string strConn = WebConfigurationManager.ConnectionStrings["conn"].ConnectionString;
    SqlConnection conn = new SqlConnection(strConn);
    conn.Open();
    SqlCommand cmd = new SqlCommand("SELECT qcat_code,qcat_name FROM dbo.question_cat " +
    "ORDER BY qcat_code", conn);
    SqlDataReader category_reader = cmd.ExecuteReader();
    while (category_reader.Read())
    {
    SqlConnection conn2 = new SqlConnection(strConn);
    conn2.Open();
    int category_code = Convert.ToInt32(category_reader["qcat_code"]);
    Cat_QSet = new Dictionary<int, Dictionary<string, string>>();
    SqlCommand cmd2 = new SqlCommand("SELECT qcat_code,quest_code, " +
    "quest_desc FROM dbo.question " +
    " WHERE qset_code =" + qsetcode + " AND qcat_code = " + category_code +
    " ORDER BY qcat_code,quest_code", conn2);
    SqlDataReader question_reader = cmd2.ExecuteReader();
    while (question_reader.Read())
    {
    Question = new Dictionary<string, string>();
    int qcode = Convert.ToInt32(question_reader["quest_code"]);
    Question.Add("quest_code", qcode.ToString());
    Question.Add("quest_desc", question_reader["quest_desc"].ToString());
    Cat_QSet.Add(qcode, Question);

    }

    if (question_reader.HasRows)
    Category.Add(category_reader["qcat_name"].ToString(), Cat_QSet);
    conn2.Close();
    cmd2.Dispose();
    }
    if (!this.IsPostBack)
    {
    current_cat = Category.Keys.ElementAt(0);
    current_quest_code = Category.Values.ElementAt(0).Keys.ElementAt(0);
    }
    cmd.Dispose();
    conn.Close();
    removeQuestion5(); removeQuestion4(); removeQuestion3(); removeQuestion2();
    removeCommentBox();
    UpdateSurveyForm();

    }
    protected void Button_Next_Click(object sender, EventArgs e)
    {
    //this is where my problem lies. the program must finish first
    //executing the method before sending the refreshed page
    getUserScores();
    Session["theUserSession"] = user;
    }

    //this is the method that needs to be finished first
    protected void getUserScores()
    {
    if (Rbtn_Q1_1.Checked)
    userScore1 = "1";
    else if (Rbtn_Q1_2.Checked)
    userScore1 = "2";
    else if (Rbtn_Q1_3.Checked)
    userScore1 = "3";
    else if (Rbtn_Q1_4.Checked)
    userScore1 = "4";
    else if (Rbtn_Q1_5.Checked)
    userScore1 = "5";
    else
    userScore1 = "";
    Label3.Text = "(In Item no: 1), pero pumasok sa question code:" + questCode1 + " userScore1: " + userScore1;
    user.insertUserRating(questCode1, userScore1);

    if (Q2Visible)
    {
    if (Rbtn_Q2_1.Checked)
    userScore2 = "1";
    else if (Rbtn_Q2_2.Checked)
    userScore2 = "2";
    else if (Rbtn_Q2_3.Checked)
    userScore2 = "3";
    else if (Rbtn_Q2_4.Checked)
    userScore2 = "4";
    else if (Rbtn_Q2_5.Checked)
    userScore2 = "5";
    else
    userScore2 = "";
    Label3.Text = "(In Item no: 2), pumasok sa question code:" + questCode2 + " userScore2:" + userScore2;
    user.insertUserRating(questCode2, userScore2);
    }

    if (Q3Visible)
    {
    if (Rbtn_Q3_1.Checked)
    userScore3 = "1";
    else if (Rbtn_Q3_2.Checked)
    userScore3 = "2";
    else if (Rbtn_Q3_3.Checked)
    userScore3 = "3";
    else if (Rbtn_Q3_4.Checked)
    userScore3 = "4";
    else if (Rbtn_Q3_5.Checked)
    userScore3 = "5";
    else
    userScore3 = "";
    Label3.Text = "(In Item no: 3), pumasok sa question code:" + questCode3 + " userScore1:" + userScore3;
    user.insertUserRating(questCode3, userScore3);
    }

    if (Q4Visible)
    {
    if (Rbtn_Q4_1.Checked)
    userScore4 = "1";
    else if (Rbtn_Q4_2.Checked)
    userScore4 = "2";
    else if (Rbtn_Q4_3.Checked)
    userScore4 = "3";
    else if (Rbtn_Q4_4.Checked)
    userScore4 = "4";
    else if (Rbtn_Q4_5.Checked)
    userScore4 = "5";
    else
    userScore4 = "";
    Label3.Text = "(In Item no: 4), pumasok sa question code:" + questCode4 + " userScore4:" + userScore4;
    user.insertUserRating(questCode4,userScore4);
    }
    if (Q5Visible)
    {
    if (Rbtn_Q5_1.Checked)
    userScore5 = "1";
    else if (Rbtn_Q5_2.Checked)
    userScore5 = "2";
    else if (Rbtn_Q5_3.Checked)
    userScore5 = "3";
    else if (Rbtn_Q5_4.Checked)
    userScore5 = "4";
    else if (Rbtn_Q5_5.Checked)
    userScore5 = "5";
    else
    userScore5 = "";
    Label3.Text = "(In Item no: 4), pumasok sa question code:" + questCode4 + " userScore4:" + userScore4;
    user.insertUserRating(questCode5, userScore5);
    }
    if(ComVisible)
    user.insertUserComment(qcat_code, TextBox_Com.Text);
    }

Similar Threads

  1. Pseudocode Problem
    By zobi316 in forum VB Classic
    Replies: 3
    Last Post: 03-10-2008, 07:05 AM
  2. Replies: 1
    Last Post: 10-01-2002, 08:21 AM
  3. Replies: 0
    Last Post: 04-05-2001, 12:12 PM
  4. refreshing when click button
    By Barbara in forum ASP.NET
    Replies: 1
    Last Post: 11-21-2000, 05:31 PM
  5. execute complete event of the connection object
    By Naveen in forum VB Classic
    Replies: 1
    Last Post: 06-09-2000, 04:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center