开发者

Determine if an asp:Placeholder is visible Jquery

I have a program with twoasp:Placeholders with runat = "server". Only one will be visible at a time. I want to determine which on is through javasacript and jquery. I have tried a few commands but none have worked. So far I have tried (these are all in an if statement:

if ($'#showClosest').visible)
if ($'#showClosest').style.visibility)
if ($'#showClosest').css.visible)
if ($'#showClosest').css.visibility)

None of them have worked/returned true, even when the place holder is visible.

Is there a way to determine if the placeholder is visible?

I need to do this through placeholders to. The info I get from the pageload will determine what is visible and what is not.

EDIT:

Here are the placeholders:

<asp:PlaceHolder ID = "showClosest" runat = "server">
<asp:PlaceHolder ID = "showSelectedInformation" runat = "server">

and here is how I set the visibilities (page load):

if (Request.Params.AllKeys.Contains(src.Config.Title))
{
     //Co开发者_开发问答de

     //Make correct divs visible
     showSelectedInformation.Visible = true;
     showClosest.Visible = false;

 }else{
     //Make sure correct divs are visible
     showSelectedInformation.Visible = false;
     showClosest.Visible = true;
 }


In answer to your question to @Mr. Disappointment, yes if you add a div inside your placeholder you can check for it. It will only be in the page markup when the placeholder is visible.

<asp:PlaceHolder runat="server" ID="myPlaceHolder">
    <div id="foo"></div>
</asp:PlaceHolder>

Then in your JavaScript you just check if the element exists.

var placeholderVisible = ($('#foo').length > 0);


The answer can be exceptionally simple in comparison to some given.

Go from this:

<asp:PlaceHolder ID = "showClosest" runat = "server">
<asp:PlaceHolder ID = "showSelectedInformation" runat = "server">

to this:

<div id="showClosest" runat="server">
<div id="showSelectedInformation" runat="server">

Now the javascript and C# that you already have will work without modification, but note that referencing IDs in your javascript from controls that are set to runat="server" require a (.NET 4.0 only) static ID rendering mode.

Oh, and make sure your jQuery looks like this:

if ($'#showClosest').is(':visible'))


Easiest way is to do it on page generation. On your ASPX page have that Javascript:

<head>
    <script type="text/javascript">
        var IsVisible = <%=PlaceHolderVisibility%>;
    </script>
</head>

Then in your code behind once you have processed the page, do:

public string PlaceHolderVisibility;

... snip ....

if(SomePlaceholder.Visible)
    PlaceHolderVisibility= "true";
else
    PlaceHolderVisibility= "false";

Something like that anyway. Note that Javascript and ASP/c# process bools slightly differently in different case so be aware of that.

Then you can simply call on the IsVisible variable when you want to know if the panel is visible or not.

With pure Jquery and none of the above solution, it's going to be hard as PlaceHolder doesn't actually render anything to the DOM. You would need an element rendered inside it that acts as a flag, but you will have to deal with instances when it isn't there/can't be found, and when it is. Can be hard to manage.


Have you tried this one?

if (jQuery('#showClosest').is(':visible')) {


Following on from @Tom Gullen's harshly-downvoted answer, if your requirement is to determine which of the placeholders on the page is visible and there will only be one visible at a time, then you can use a property to return the ID of the visible one:

public string VisiblePlaceHolderID
{
    get
    {
        return this.Controls.OfType<PlaceHolder>().FirstOrDefault(p => p.Visible == true).ClientID;
    }
}

This will return the ID of the first placeholder on the page that has Visible="true", which you can then use directly in your JavaScript, something like this:

alert("<%= this.VisiblePlaceHolderID %>");


OK - Javascript and jQuery won't work directly with an ASP.NET PlaceHolder control. This is because such a control is server-side only, and doesn't get rendered directly to the client but rather only what is either within it, or overriding it, so:

<asp:PlaceHolder runat="server" ID="myPlaceHolder">
    ???????????
</asp:PlaceHolder>

Is output as:

"



   ???????????


"

Spaces, and all. Therefore, the identifier you use in jQuery ($'#placeHolder') has no standing here as it doesn't identify anything. So, the short answer goes like this:

You can't determine directly, using jQuery, whether or not the PlaceHolder.

You might want to take some level of interest in @Tom's answer and he seems to have provided a script / client-side solution, albeit venturing away from the languages starting with j. ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜