1

i'm trying to do this assigning an method to the event on the checkbox tag like this:

    OnCheckedChanged="ShowDiv"

However, i'm not succesfull at all. How to do it? Here's my method:

    public void ShowDiv(object sender, System.EventArgs e) 
    {
        var div = FindControl("ListaEmOutrosDocumentos") as HtmlGenericControl;
        var checkbox = FindControl("Principal") as CheckBox;

        if(checkbox.Checked == true)
        {
            div.Style.Clear();
            div.Style.Add("display","block");
        }
        else
        {
            div.Style.Clear();
            div.Style.Add("display","none");
        }
    }
6
  • 1
    Is the checkbox nested inside something else? Commented Oct 15, 2012 at 21:36
  • Does the display style attribute get rendered when the page re-renders? What value does it have? Commented Oct 15, 2012 at 21:39
  • Also, I assume that you are posting back after checking the checkbox, either by setting AutoPostBack or having a separate postback trigger, such as a button. Is this so? Commented Oct 15, 2012 at 21:40
  • @IrishChieftain The checkbox is inside a div. Commented Oct 15, 2012 at 21:42
  • @AnnL. I didn't think about de AutoPostBack setting. I'll try that! Commented Oct 15, 2012 at 21:44

3 Answers 3

2

You might consider using an ASP.NET Panel control instead of a div. That will have a Visible property that you can set.

Sign up to request clarification or add additional context in comments.

1 Comment

It may introduce possible side effects. When the Visible property is set to false, the control is not rendered at all. display=none does a bit different thing: the control is rendered, but the visibility is controlled by CSS.
1

Quick way using server side code only

Aspx

<div id="myDiv" runat="server" style="height:200px;width:100px;background-color:Blue"></div>
<asp:CheckBox ID="chkShowHideDiv" runat="server" AutoPostBack="True" 
    oncheckedchanged="chkShowHideDiv_CheckedChanged" Text="Hide Div"  />

Code behind

 protected void chkShowHideDiv_CheckedChanged(object sender, EventArgs e)
{
    myDiv.Visible = chkShowHideDiv.Checked ? false : true;
}

Comments

1

I did this and it worked.

<div id="ListaEmOutrosDocumentos" runat="server">
            <asp:CheckBox runat="server" ID="Principal" AutoPostBack="True" OnCheckedChanged="ShowDiv"/>
        </div>

Code Behind

public void ShowDiv(object sender, System.EventArgs e)
    {
        var div = ListaEmOutrosDocumentos as HtmlGenericControl;
        var checkbox = sender as CheckBox;

        if (checkbox.Checked == true)
        {
            div.Style.Clear();
            div.Style.Add("display", "block");
        }
        else
        {
            div.Style.Clear();
            div.Style.Add("display", "none");
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.