1

I'm working on some legacy inline Web Forms code. I have added a checkbox which is set by default. The issue is when the checkbox is unchecked, it still flips to checked when the form is submitted. There are no page event handlers per say.

In the JavaScript, if I set an alert it displays the value as blank which is correct when the checkbox is unchecked. However, the checkbox always reverts to its checked state when the form is submitted.

Markup:

<input type="hidden" id="inYard" name="inYard" runat="server" />
<input type='checkbox' id="excludeYard" name='excludeYard' onclick="CheckYardSelection()" 
    runat="server" />Include Items in Yard

JavaScript:

function CheckYardSelection()
{
    var yardCheckbox = document.getElementById('excludeYard');

    if (!yardCheckbox.checked) 
        document.getElementById('inYard').value = "";
    else
        document.getElementById('inYard').value = "Y";
}

C#:

<script runat="server">
...
    if(!String.IsNullOrEmpty(Request.Form["inYard"]))
    {
        if (Request.Form["inYard"].ToString() == "Y")
        {
            excludeYard.Value = Request.Form["inYard"].ToString();
            excludeYard.Checked = true;
        }
    }
...
</script>
IrishChieftain
  • 15,108
  • 7
  • 50
  • 91

1 Answers1

1

You don't post your complete code but if you implement everything this way, you shouldn't get this issue anymore:

aspx file:

<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebForms._Default" %>

<script src="Default.js" language="javascript" type="text/javascript"></script>

<form runat="server">
    <input type="hidden" id="inYard" name="inYard" runat="server" />
    <input type='checkbox' id="excludeYard" name='excludeYard' onclick="CheckYardSelection()" runat="server" />

    <input type="submit" value="Post" />
</form>

Code behined:

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(Request.Form["inYard"]))
        {
            if (Request.Form["inYard"].ToString() == "Y")
            {
                excludeYard.Value = Request.Form["inYard"].ToString();
                excludeYard.Checked = true;
                // query += " AND IC.NewWellPad <> 'YARD'";
            }
        }
    }
}

Default.js

function CheckYardSelection() {
    var yardCheckbox = document.getElementById('excludeYard');

    if (!yardCheckbox.checked)
        document.getElementById('inYard').value = "";
    else
        document.getElementById('inYard').value = "Y";
}
Arman Ebrahimpour
  • 4,252
  • 1
  • 14
  • 46
  • I removed the checked and value attributes, still same result. Also tried using a hidden variable to toggle logic in C# to no avail - hidden value set in JS was coming up empty in C#. Will update the question, but problem persists. – IrishChieftain May 26 '20 at 19:22
  • @IrishChieftain: Please pay attention to last paragraph of my answer. It's vital. You have to set checkbox checked state in `Load` event of JavaScript code of page (value ="Y" or not based on your current state) – Arman Ebrahimpour May 26 '20 at 19:29
  • Can you give me a code snippet? I'm not 100% sure what you mean by the load event of the JS code. – IrishChieftain May 26 '20 at 19:33
  • 1
    @IrishChieftain Before any further action lets check https://stackoverflow.com/a/1992745/9212040 and ensure that your uchecked checkbox also sent to server correctly. if it wasn't the case then I'll post my idea – Arman Ebrahimpour May 26 '20 at 20:08
  • I can confirm that the unchecked checkbox is not being posted back. I checked the Request Form variables. – IrishChieftain May 26 '20 at 20:36
  • I updated the question to include the hidden variable. To reiterate, even now, when I check the checkbox and submit the form the checkbox renders as unchecked. – IrishChieftain May 26 '20 at 21:04
  • 1
    There are no code-behind files but your solution worked inline. Thanks for your help Arman! – IrishChieftain May 27 '20 at 11:43