Home > Software design >  Image appears after checking checkbox in HTML
Image appears after checking checkbox in HTML

Time:10-22

protected void btnSubmit_Click(object sender, EventArgs e)
    {

        imgPurple.Visible = true;
    }

For now, I am able to make the imgPurple appear after I click btnSubmit. However, thats not what I want.

I want the imgPurple to appear when I check a checkbox, not button. How do I achieve that?

CodePudding user response:

I'm no expert in C# or .NET(If that's even the language it is), but if you compare the previous values with the current values and they are not the same, then they have changed.

So save the previous state into an object, and compare that object with the current state.

Another note, since you're not using a button as the event target, you'll probably want to use something like @Bobney was suggesting as the event handler like OnCheckedChanged as opposed to BtnSubmit_Click.

CodePudding user response:

HTML with JavaScript:

<html>
<head>
<script>
function showPic()
{
    if (document.getElementById("myCb").checked === true)
    {
        //document.getElementById("myPic").style.display = "inline";
        document.getElementById("myPic").style.visibility = "visible";
    }
    else
    {
        //document.getElementById("myPic").style.display = "none";
        document.getElementById("myPic").style.visibility = "hidden";
    }
}
</script>
</head>
<body onload="showPic();">

<input id="myCb" type="checkbox" onclick="showPic();" /><br />
<br /> 
<img id="myPic" src="myPic.png" alt="myPic" />

</body>
</html>

CodePudding user response:

Do you have AutoPostBack set to true? The button you have tried will cause a postback but a checkbox change won't postback by default. This works:

<asp:CheckBox ID="CheckBox1" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" runat="server" />

With the following:

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        imgPurple.Visible = true;
    } 
  • Related