ListBox server side visibility with C # and ASP.NET

I'm interested in using the onmouseover event to create and disappear a list. I'm new to ASP.NET and I don't want to write javascript yet. I am trying to use the following code and its color changing part works, but list visibility doesn't work:

if (! IsPostBack) {Button2.Attributes.Add ("onmouseover", "this.style.backgroundColor = 'Red', ListBox3.style.visibility = 'visible'"); }

        if (!IsPostBack)
        {
            Button2.Attributes.Add("onmouseout", "this.style.backgroundColor='Blue', ListBox3.style.visibility='hidden'");
        }

      

I've tried this code with and without "PostBack" but no luck so far. Does anyone see where my code is putting up with me?

Thank,

DFM

+1


source to share


1 answer


Try:

    if (!IsPostBack)
    {
        btnHide.Attributes.Add("onmouseout", "this.style.backgroundColor='Blue';ListBox3.style.display='none'");
        btnShow.Attributes.Add("onmouseover", "this.style.backgroundColor='Red';ListBox3.style.display='block'");
    }

      

The visibility property works a little differently than the display property. When the visibility property is set to "hidden", the element is hidden but the layout is not affected, while setting the display property to "none" completely removes the element that might affect the layout.

If you want to change the visibility of the list without affecting the layout, you can use a div as a wrapper and then change its visibility property.



<div id="wrapper">          
    <asp:ListBox ID="ListBox3" runat="server"></asp:ListBox>            
</div>
<asp:Button ID="btnShow" runat="server" Text="Button" />
<asp:Button ID="btnHide" runat="server" Text="Button" />

      

Modify ASPX to toggle the visibility property of the div element containing the list box.

if (!IsPostBack)
{
    btnHide.Attributes.Add("onmouseout", "this.style.backgroundColor='Blue';wrapper.style.visibility='hidden'");
    btnShow.Attributes.Add("onmouseover", "this.style.backgroundColor='Red';   wrapper.style.visibility='visible'");
}

      

+2


source







All Articles