locked
season checkbox RRS feed

  • Question

  • User-1683022223 posted

    Hello ,

    i would like make a season for checkbox

    if i check = true

    if unchecked = false

     protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
            {
    
            }

    Friday, February 12, 2021 6:47 PM

Answers

  • User-1683022223 posted

    i found it

    HTML

      <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" Text="Location" />

    C#

     protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
            {
    
                Session["check_location"] = CheckBox1.Checked;
    
            }

    Second page with session

    HTML

     <asp:Label ID="chec_location" runat="server"></asp:Label>
    

    C#

    protected void Page_Load(object sender, EventArgs e)
            {
                chec_location.Text = Session["check_location"].ToString();
     if (!this.IsPostBack)
                {
    
                }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 17, 2021 10:03 PM

All replies

  • User-939850651 posted

    Hi kankonq8,

    The CheckBox in the WebForm has checked attribute to determine whether the CheckBox is selected.

    If you want to determine the state of the CheckBox in the CheckedChanged event, you need to add the attribute AutoPostBack="true" to the control.

    Something like this:

    <form id="form1" runat="server">
            <div>
                <asp:CheckBox Text="spring" ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox3_CheckedChanged" AutoPostBack="true" />
                <br />
                <asp:CheckBox Text="summer" ID="CheckBox2" runat="server" OnCheckedChanged="CheckBox3_CheckedChanged" AutoPostBack="true" />
                <br />
                <asp:CheckBox Text="autumn" ID="CheckBox3" runat="server" OnCheckedChanged="CheckBox3_CheckedChanged" AutoPostBack="true" />
                <br />
                <asp:CheckBox Text="winter" ID="CheckBox4" runat="server" OnCheckedChanged="CheckBox3_CheckedChanged" AutoPostBack="true" />
                <br />
                you have checked:
                <asp:Label ID="resultLabel" runat="server"/>
            </div>
        </form>
    
    protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
            {
                string text = "";
    
                if (CheckBox1.Checked)
                    text += "spring ";
                if (CheckBox2.Checked)
                    text += "summer ";
                if (CheckBox3.Checked)
                    text += "autumn ";
                if (CheckBox4.Checked)
                    text += "winter";
    
                resultLabel.Text = text;
            }

    Result:

    Hope this can help you.

    Best regards,

    Xudong Peng

    Monday, February 15, 2021 2:21 AM
  • User-1683022223 posted

    i found it

    HTML

      <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" Text="Location" />

    C#

     protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
            {
    
                Session["check_location"] = CheckBox1.Checked;
    
            }

    Second page with session

    HTML

     <asp:Label ID="chec_location" runat="server"></asp:Label>
    

    C#

    protected void Page_Load(object sender, EventArgs e)
            {
                chec_location.Text = Session["check_location"].ToString();
     if (!this.IsPostBack)
                {
    
                }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 17, 2021 10:03 PM