0

I have a checkbox in a form. When I click the only button on the form, it calls a JS function. I put some logic in the function that alerts me if the checkbox is selected. If the checkbox is selected, it comes back true. Otherwise it comes back false. The JS alert works perfectly as intended.

However, after the postback from the button being clicked, I have logic pertaining to the Checkbox in C#. I have put the logic everywhere in the page and still cannot figure out the problem.

if (myCheckbox.Checked)
{
  // Certain code is implemented.
}
else
{
  // Other code is implemented.
}

Right now I have the code in the Page_LoadComplete(object sender, EventArgs e). I have verified in the code behind .cs file, any code pertaining to the status of the Checkbox (example: myCheckbox.Checked = false) is commented out. I have been looking at this around the clock for two days and it is driving me crazy. How is it possible I am getting a false value after the postback when the Javascript says it is selected (when it is selected) and not selected (when it is not selected).

  • I tried putting the logic (if (checkBox.checked)) everywhere in the page and am getting the same result.

  • I tried calling a JS function from the C# code behind file but found out I can't return a value that way.

  • I tried using a HiddenField value to store the status of the Checkbox and that was unsuccessful as well.

  • I've searched the internet trying similar things other people used but had no luck.

5
  • you want to use OnCheckChanged: <asp:CheckBox ID="A1" runat="server" OnCheckedChanged="A1_CheckedChanged" AutoPostBack="True" />
    – smoore4
    Commented Sep 11 at 21:51
  • The checkbox is made in condebehind or not? Could you show your aspx and cs code?
    – Orion77
    Commented Sep 11 at 22:25
  • @smoore4 So, I don't want to do a postback right away when a user selects the checkbox. There are some other items on the page (parameters/values) users can move around/pick. Is there a way to keep the current status of the page but just call the one function from the autopostback - where I can write the code for: Checbox.selected = true. Commented Sep 12 at 16:55
  • @Orion77 Sorry for the late response, was stuck in meetings. This is government work unfortunately so I can't share the aspx and cs code. That's the worst part about this job. But the checkbox is made in the aspx file: <asp:CheckBox ID="nameOfCheckbox" runat="server" Text="Just some text" Style="some styling" /> And obviously the checking of the code that I listed above is in the .cs file. Never in my life did I think Id have an issue with a simple checkbox. Commented Sep 12 at 17:06
  • i see. the button that calls js...it should use OnClientClick and you should have a different OnClick where you can check your checkbox. same button, two events
    – smoore4
    Commented Sep 12 at 20:39

1 Answer 1

0

I did a simple code to reproduce your issue, but i didn't

this is the aspx file

<%@ Page Language="C#"  AutoEventWireup="true" CodeBehind="home.aspx.cs" Inherits="WebApplication1.home" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
        <form id="form1" runat="server">
            <div>
                <asp:CheckBox ID="nameOfCheckbox" runat="server" Text="Just some text" Style="font-family: 'Times New Roman', Times, serif" /><br />
                <asp:Button Text="button with js and not postback" runat="server" OnClientClick="RunJs('No Postback');return false;" /><br />
                <asp:Button Text="button with js and postback no method" runat="server" OnClientClick="RunJs('Postback without method in cs file');" /><br />
                <asp:Button text="button without js and postback with method" runat="server" OnClick="Unnamed_Click" /><br />
                <asp:Label runat="server" ID="LBLCheckedFromMethod" Text="Is checked in asp page?"></asp:Label>
            </div>

            <script>
                function RunJs(value) {
                    alert(value);
                }
            </script>
        </form>
</body>
</html>

and this is the cs file

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

namespace WebApplication1
{
    public partial class home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (nameOfCheckbox.Checked)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('checked in asp page')", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('not checked in asp page')", true);
            }
        }

        protected void Unnamed_Click(object sender, EventArgs e)
        {
            if (nameOfCheckbox.Checked)
            {
                LBLCheckedFromMethod.Text = "Is checked in asp page? True";
            }
            else
            {
                LBLCheckedFromMethod.Text = "Is checked in asp page? False";
            }
        }
    }
}

First button raise RunJs script and do not postback

Second button raise RunJs and do a postback that page_load catch

Third button raise RunJs do a postback and call method that set true or false in the label.

Page_Load read ever the checkbox status correctly...

Pls send more info about your code...so we can help you :)

3
  • Thank you so much for your help! Unfortunately, the issue is not resolved. So I decided to added a HiddenField. The necessary logic for it works and has been tested using alerts. Going off the JS alerts, it is not having any issues being assigned the correct value. My only question now is (since I know it's not the Checkbox being an issue, due to the HiddenField having the same issue) Would there be any reason when I change the value of an object in JS - the value does not hold when doing a postback? I'm just baffled at this point to be honest. Ex: HiddenField.Value = "Selected"; Commented 2 days ago
  • I'm wondering, is it possible the page is doing a refresh instead of a postback and this is why I am losing the changes? And if this is the case, what is the best solution? Commented 2 days ago
  • In the example i did page postback with refresh page, no postback of page and checkbox don't lose his value.. i'm thinking you have in cs code, somewhere, a calling that erase the checked value on postback... are you sure isnt'it?
    – Orion77
    Commented yesterday

Not the answer you're looking for? Browse other questions tagged or ask your own question.