locked
Button Click count where count need to be transferred to other page. RRS feed

  • Question

  • User-282579391 posted

    Hello,

    I have in my project radio buttons that the user can select and a button to count the number of time a radio button is selected. I declared a counter variable, but it is not counting more than one and I 'm not to sure why. I also need to keep the count as I want the result to be transferred to an other page but I am not there yet. My priority is to have the counter working for now. 

    Here is my code so far: 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Drawing;
    
    
    namespace DuckShop
    {
        public partial class ShoppingCart : System.Web.UI.Page
        {
    
    
            int NumberOfYellowClick = 0;
            int NumberOfRedClick = 0;
            int NumberOfGreenClick = 0;
    
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
    
               
    
                if (!this.IsPostBack)
                {
                    RadioButtonListDuck.Items.Add("Yellow");
                    RadioButtonListDuck.Items.Add("Red");
                    RadioButtonListDuck.Items.Add("Green");
    
                    // Set the picture.           
                    ImageDuck.ImageUrl = "img/canard.png";
    
    
    
                    ViewState["NumberOfYellowClick"] = 0;
                    ViewState["NumberOfRedClick"] = 0;
                    ViewState["NumberOfGreenClick"] = 0;
    
                }
    
    
            }
    
            protected void RadioButtonListDuck_ServerSelectedIndexChanged(object sender, EventArgs e)
            {
                if (RadioButtonListDuck.SelectedIndex == 0)
                {
                    ImageDuck.ImageUrl = "img/YellowDuck.png";
                    
                    Label1.Visible = true;
                    Label1.Text = "Nice yellow!";
                }
                else if (RadioButtonListDuck.SelectedIndex == 1)
                {
                    ImageDuck.ImageUrl = "img/RedDuck.png";
                    Label1.Visible = true;
                    Label1.Text = "Awesome Red!";
                }
    
                else if (RadioButtonListDuck.SelectedIndex == 2)
                {
                    ImageDuck.ImageUrl = "img/GreenDuck.png";
                    Label1.Visible = true;
                    Label1.Text = "Great Green!";
                }
    
            }
    
            protected void BtnAddToCart_ServerClick(object sender, EventArgs e)
            {
    
    
                if (RadioButtonListDuck.SelectedIndex == -1)
                {
                    Label1.Visible = true;
                    Label1.Text = "Please select a duck colour to add to the cart.";
                    Label1.ForeColor = Color.Red;
                }
                else if (RadioButtonListDuck.SelectedIndex == 0)
                {
                    NumberOfYellowClick = NumberOfYellowClick + 1;
                   LlbNumberItem.Text = NumberOfYellowClick.ToString();
                }
    
                else if (RadioButtonListDuck.SelectedIndex == 1)
                {
                    NumberOfRedClick++;
                    LlbNumberItem.Text = NumberOfRedClick.ToString();
                }
    
                else if (RadioButtonListDuck.SelectedIndex == 2)
                {
                    NumberOfGreenClick++;
                    LlbNumberItem.Text = NumberOfGreenClick.ToString();
                }
            }
    
    
        }
    }

    I would really appreciate if someone would have a look at it.

    Thank you,

    Marie 

    Sunday, June 5, 2016 4:44 AM

All replies

  • User-286291038 posted

    Hi Marie,

    Instead of triggering a server side post event for each click on the radiobutton, why not use javascript/jquery to update a counter for each time the user clicks on the radio button?

    You can pass on this count to the server whenever you are ready to process the user radio button click count.

    Reference,

    http://stackoverflow.com/questions/13896249/count-number-of-clicks-on-radio-button

    http://stackoverflow.com/questions/8413828/how-to-handle-radio-button-list-option-change-with-jquery

    http://forums.asp.net/t/1832606.aspx?jquery+for+radiobuttonlist+change+and+click+functions

    Sunday, June 5, 2016 4:59 AM
  • User-282579391 posted

    Hello Nataraj ,

    Thank you for such a quick reply. The reason why I am not going with  javascript/jquery is because I have been asked to manage state mode with asp.net. 

    Thank you,

    Marie

    Sunday, June 5, 2016 5:13 AM
  • User-286291038 posted

    Hi Marie,

    Not sure what you mean by that. But I guess you should be able to achieve your requirement using client side implementation in this case too.

    Sunday, June 5, 2016 6:06 AM
  • User36583972 posted

    Hi Marie-Andree,

    I also need to keep the count as I want the result to be transferred to an other page but I am not there yet. My priority is to have the counter working for now. 

    You can use Response.Redirect (url) and pass your count values to another page.

    Current page:

       static int NumberOfYellowClick = 0;
               static int NumberOfRedClick = 0;
               static int NumberOfGreenClick = 0;
               string s_url;
                s_url = "WebForm70.aspx?NumberOfYellowClick=" + NumberOfYellowClick.ToString() + "&" + "NumberOfRedClick=" + NumberOfRedClick.ToString();
                Response.Redirect(s_url);

    Another page:

        protected void Page_Load(object sender, EventArgs e)
            {
                Label1.Text= Request.QueryString["NumberOfYellowClick"].Trim() +"<>"+ Request.QueryString["NumberOfRedClick"].Trim();
            }

    Also, I suggest you can use the server session to store the count data and use in the life of the app domain.

    Exploring Session in ASP.NET:

    http://www.codeproject.com/Articles/32545/Exploring-Session-in-ASP-Net

    Best Regards,

    Yohann Lu

    Monday, June 6, 2016 5:53 AM