views:

48

answers:

8

Hey all, having an issue getting asp buttons to interact with JQuery. I'm basically trying to hide a div that contains a form and replace it with an processing image. It works fine for me when I use an HTML input button as the trigger but when I use an aspButton nothing happens.

This works (the id of the HTML button is 'btnSubmit'):

<script>
    $('#btnSubmit').click(function () {
        $('#form1').fadeOut('fast', function () {
            $('#processing').fadeIn('fast', function () {
            });
        });
    });
</script>

This doesn't (the id of the ASP button is 'btnSubmitASP'):

<script>
    $('#btnSubmitASP').click(function () {
        $('#form1').fadeOut('fast', function () {
            $('#processing').fadeIn('fast', function () {
            });
        });
    });
</script>

Any idea what the trick is to get the asp button to do this?

Thanks

A: 

My knowledge of jQuery is very shallow, but I can give you one tip: Remember that jQuery is being executed client-side, while the ASP button is rendered on the server and returned in the response.

Double-check the HTML markup for the button when your page is returned from the server, and make sure it's structured as you expect. Perhaps the ID attribute isn't being set as expected, for example.

djacobson
+4  A: 

The ASP.net server ID for the control is different from the html ID. (ASP.net calls this the client ID). You can get the client id this way:

$('#<%= this.btnSubmitASP.ClientID %>').click( /* etc */ );
recursive
I didn't know that. Thanks for the info. But it still causes problem if you want to use the ID for styling in CSS.
Gabriel
That's true. You can work around it by using css classes instead, (CssClass property in asp.net) or wrapping the button in a div with an id or other approach.
recursive
This worked great. Thanks for the quick help everyone... you guys are awesome!
markiyanm
A: 

Your button controller is runat="server" so this means that .NET will modify the controller's id before rendering it in HTML.

jQuery tries to use that ID to do whatever you want to do with it. But the ID is no longer the same.

Use a class instead on your button. I know it's not as fast as an ID, but it's the best way to do it because .NET will not modify your css class.

Gabriel
+1  A: 

For ASP.NET buttons you should use the OnClientClick property as it has built in client side scripting added to the button to do its post back behavior. Example:

<asp:Button ID="btnSubmitASP" runat="server"
    OnClientClick="yourJqueryFunction();" />

If you return false in the OnClientClick you will prevent the default behavior of the button preventing a PostBack. Doing nothing or returning true will cause the PostBack to occur. By using this method you don't need to know the name of your Button to attach the script code.

To just get your code working though, you need to get the ClientID of the control inline to creating you script so change the following line to use the ClientID property of the Button:

$('#<%= btnSubmitASP.ClientID %>').click(function () {   

You need to get the ClientID because ASP.NET adds to name to namespace it and prevent duplication of names. If you look at the ASP.NET Button, the you will notice the name and ID properties have a lot more added to it like:

<input type="submit" name="ctl00$ContentPlaceHolder1$btnSubmitASP" value="Test"
    id="ctl00_ContentPlaceHolder1_btnSubmitASP" />
Kelsey
+2  A: 

Try this:

<script>
    $('<%=btnSubmitASP.ClientID%>').click(function () {
        $('#form1').fadeOut('fast', function () {
            $('#processing').fadeIn('fast', function () {
            });
        });
    });
</script>

Explanation: ASP.NET renames all of its controls when they get sent to the client. Consequently, your ASP.NET Button does not have a client ID of "btnSubmitASP" client-side. The above code calls the server control on the server side and gets its client-id to use in the jQuery code.

Alternatively, you can use jQuery selectors:

<script>
    $("[id$='_btnSubmitASP']").click(function () {
        $('#form1').fadeOut('fast', function () {
            $('#processing').fadeIn('fast', function () {
            });
        });
    });
</script>

This will look for controls whose client ID ends with "_btnSubmitASP".

Matthew Jones
+3  A: 

If you are using asp.net 4.0 you can set the button's ClientIDMode property ='Static'. This will stop the runtime from mucking with the ID.

DancesWithBamboo
3.5 for now unfortunately... good to know for the future though
markiyanm
+1  A: 

Another alternative to using the ClientId is to assign a unique class to the ASP:button. Your selector would then look like this:

<asp:button runat="server" CssClass="submitbutton">/<asp:button>

<script>
        $("submitbutton").click(function () {
            $('#form1').fadeOut('fast', function () {
                $('#processing').fadeIn('fast', function () {
                });
            });
        });
 </script>
David Robbins
A: 

If your ASP:Button contains runat="server" then .NET will modify the ID value before it get to the DOM, so your resulting <input> will probably wind up looking like

<input id="ctl00_ContentPlaceHolder1_btnSubmitASP" />

Therefore, your jQuery selector $('#btnSubmitASP') is no longer valid because the ID has changed.

Use Firebug or Right click -> View source to confirm the actual ID value.

Phil Scholtes