I can't click on an ASP:Button if it is hidden

2019-04-06 21:52发布

问题:

I have found StackOverFlow answers and other resources saying that you can click on a hidden ASP:Button with jQuery by

$("#<%=HiddenButton.ClientID%>").click();

or

$("#<%=HiddenButton.ClientID%>").trigger("click");

However, neither of these are working for me UNLESS the button is Visible="true"

Here is the button:

<asp:Button ID="loadCustomerContacts" runat="server" OnClick="loadCustomerContacts_Click" visible="false" />"

回答1:

If you set the Visible property to false; typically in .net the control will not be rendered in the HTML output after the page is processed. Therefore as far as jQuery is concerned, the button does not exist.

You can do a View Source on the page to verify this.

If you want to do this, instead of using the Visible property, you can do something like:

<asp:Button ID="myButton" runat="server" style="visibility: hidden; display: none;" />

Or you can assign it a CSS class that hides it.



回答2:

You need to add style="display:none" to the button instead of Visible=False



回答3:

Coding Gorilla is right, however, what you can do is instead of setting the Visible property, add this to the tag instead:

style="display:none;"

This will hide the button in CSS instead of not rendering the page.



回答4:

When the Visible is false the button is not rendered in the browsers. If it is not in the browser, it can't be clicked. Instead of using Visible attribute use CssClass to hide it. Create a class like in the stylesheet

.Hidden {
    display:none;
}

and then use

loadCustomerContacts.CssClass = "Hidden"


回答5:

That's probably because the button never gets rendered into the page markup, although it exists in the page object's control hierarchy. Client-side JS code relies on the existing markup and has nothing to do with what's available in the ASP page model.

If Visible=false does not work, have you tried adding something like "display=none;" to the button's style? If the button is physically on the page but invisible your Javascript method could work.