0

how to check does the asp. net control element exist or not? My page actually don't have the element with ID "hyper", but i just want to verify the existence of this.

im looking for the asp.net element with ID "hyper".

so what i have tried is

if (("#<%=hyper.ClientID %>").exists())

or

if (("#<%=hyper.ClientID %>").length>0)

but it give me this error

The name 'hyper' does not exist in the current context

<script type="text/javascript">
        function getElement() {
            if ($('#<%=(hyper.ClientID)%>').length > 0) {
                alert("none!");
            }
            else
                alert("exist!");

        }
</script>

<asp:HyperLink ID="hyper2" runat="server" NavigateUrl="www.facebook.com" >click me</asp:HyperLink>
5
  • Maybe just a typo, but your Hyperlink is hyper2 and your code is looking for hyper Commented Sep 6, 2011 at 9:22
  • i do it purposely. Supposingly it will alert "none" instead of "exist!". Right? Commented Sep 6, 2011 at 9:32
  • well no because your code is first trying to get the client id of a server control Commented Sep 6, 2011 at 9:45
  • you must specify hyper2.ClientID inside getElement() for it to work Commented Sep 6, 2011 at 11:20
  • @Razvan, can you raise me an example? Commented Sep 7, 2011 at 0:50

4 Answers 4

1

The issue is that the element "hyper" doesn't exists, therefore ".ClientID" returns an error. You have to check for the element first:

(VB.Net)

<% If hyper IsNot Nothing Then %>
   ..: $("<%= hyper.ClientID %>").etc.
<% End If %>

(C#)

<% if(hyper != null) { %>
  ..: $("<%= hyper.ClientID %>").etc
<% } %>

I haven't tried the C# inside of an ASPX page, but the VB.Net works.

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

Comments

0

try using this,

 var Id= "<%=hyper.ClientID %>"
  $('#'+Id).exists()

Comments

0

Try

if ($('#<%=(hyper.ClientID)%>').length > 0 ) {
     // exists
}

added in the $ to the code.

working example here http://jsfiddle.net/h3UUY/

2 Comments

it give me the same result :(
what is hyper? could you post your html markup with the .net control?
0

See this: What is best method to find a ASP.Net control using jQuery?

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.