1

I want to get the id of an image button inside of a gridview that is inside an updatepanel.

 $("<%# gvLineItems.ClientID %> tr img[id*='ibDelete']").click(function() {
                                                                alert('hi');
                                                                $(this).fadeOut(1000, function() {
                                                                    $(this).remove();
                                                                });       
                                                         });

What I want to do is upon clicking a delete button I want to fade the row out of my grid view then completly remove it.

This is not workign but is not throwing any errors. I have this inside of $(document).ready()

Does anyone know what I am doing incorrectly?

2 Answers 2

1

Firstly you have a small typo - remove the # from the asp script snippet as follows

$("#" + "<%= gvLineItems.ClientID %> tr img[id*='ibDelete']")

You cannot bind an event to an element with the same id, even if it is in a different container.

$("<%# gvLineItems.ClientID %> tr img[id*='ibDelete']")

id="ibDelete" is a NO-NO.

Just change this to a class definition on the image and call it using 'img[class=lbDelete]'.

Finally, when you are usng inline scripting anywhere in the page, the operator '#' or '=' mean different things in terms of when and how they are executed. But like you pointed out, you will get an error when a client side script tries to use inline script tag content which is generated on the fly.

To resolve this, simple put the entire script tag into a server side control as such:

<div runat="server">
    <script type="text/javascript">
        .....
    </script>
</div>

Lemme know if you need anymore help, Cheers!

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

2 Comments

no your first issue will not work, it causes the following to be thrown The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
hey, no problem, that error is coming because you have put the script as-is in the page. to fix it, just put the whole script tag inside a DIV which is a server side control. that way should work just fine.
1

It appears that you're grabbing the client id of the control incorrectly. Try this:

$("#" + "<%# gvLineItems.ClientID %> tr").find('#ibDelete').click(...)

EDIT:

Per our discussion in chat:

$(".TempName").click(function() { 
    $(this).closest("tr").fadeOut(10000, function() { 
        $(this).remove(); 
    }); 
});

6 Comments

Now when I run my site I get an error : The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Sry, typo. It should be <%# not <%=. The important thing is that you prepend the hash to the ID.
Still not executing the jquery, no script errors, but it does not do the alert or the fade effect.
It sounds like you have a selector problem. Try out my edit above. Even better, assign the image a class and select by class. If you can post the markup that's rendered, I could write the selector for you.
|

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.