Victor
Victor

Reputation: 941

jQuery server-side button

I have a page(ASP.NET 3.5) with a number of buttons, some of them are saving buttons, some are not. I have to be generic, i.e. cannot call individual controls by ID. My save buttons will probably have attribute UserSubmitBehavior = true. In a click event handler I need to distinguish what type of button I pressed, so:

$('input[type=submit]').bind('click', function(e)
{
    //need to know how to set up this condition
    if (this.type = "submit")
    {

          //do something here 
    }
    else 
    {
          //do something else

    } 


}); 

How can I do that?

Upvotes: 1

Views: 484

Answers (3)

Jagd
Jagd

Reputation: 7307

I've done this little trick on occassion where I've needed to identify labels or textboxes within a GridView control. What I'll do is add a custom attribute to help me identify them on the page. For example:

<asp:Label ID="lblSpecial" runat="server" Text="Whatever" MyCustomeAttr="SpecialLabel">
<asp:Label ID="lblSpecial2" runat="server" Text="Whatever" MyCustomeAttr="SpecialLabel2">

It's fairly simple from there to use JQuery to get at that custom attribute, be it by the .attr() function or by a selector:

$("span[id$='_lblSpecial2'][MyCustomeAttr='SpecialLabel2']");

Upvotes: 0

munch
munch

Reputation: 6321

If I understand correctly, you have multiple types of buttons, both regular and submit. You can do it in one function, like so:

$('input[type=submit], input[type=button]').bind('click', function(e)
{
    if ($(this).attr('type') == "submit")
    {
          //do something here 
    }
    else 
    {
          //do something else
    } 
}); 

You can also break it up into a more readable manner though:

$('input[type=button]').bind('click', function(e){
   //do button stuff here
});

$('input[type=submit]').bind('click', function(e){
   //do submit stuff here
});

Personally, I prefer the second method.

Upvotes: 3

recursive
recursive

Reputation: 86074

You could use a css class:

<asp:Button CssClass="SubmitButton" ... />

and:

$('input.SubmitButton').bind( .. );

Will this work for you?

Upvotes: 0

Related Questions