Anton Belev
Anton Belev

Reputation: 13523

jQuery Mobile button click event not working

I'm new to JQuery Mobile so excuse me for this probably easy question.

I have a button:

<a id="btnSort" href=# data-role="button"
                runat="server" onclick="Click_btnSort">Sort</a>

and code-behind event handler:

protected void Click_btnSort(object sender, EventArgs e)
        {
            ...
        }

I've got a breakpoint at the beginning of this method, however it does not fire when I click on the button.

PS. I'm not using any field validators.

Any suggestions?

Upvotes: 0

Views: 7224

Answers (3)

Behzad G
Behzad G

Reputation: 11

Although this is an old post what you can do is create a

<asp:Button ID="btnServerSort" style="display:none;"  runat="server" Text="SORT" OnCommand="Click_btnServerSort" ..>  

with an associated server side event.

Then modify the code as follow

<a id="btnSort" href=# data-role="button" data-ajax="false" runat="server"  onserverclick="Click_btnServerSort"   >Sort</a>

I hope this helps.

Upvotes: 0

AshOoO
AshOoO

Reputation: 1148

As <a> tag is not related to serverside controls so I suppose that can't happen like that way.

your code even with run at server is still will look for the Click_btnSort in javascript function no the one in code behind so you should add a function in script/javascript tag with the name you will call in onclick event.

Upvotes: 1

Jeroen1984
Jeroen1984

Reputation: 1686

The reason your event is nog firing is because you use a html a-element, that element does not trigger a postback (maybe it does when you set the elements autopostback proerty to true, not sure if this works for a-elements).

When you want to use the ASP.NET button click event in code behind (to do server side stuff when clicking), you probably better use a ASP:Button or LinkButton element, which works out of the box.

When you want to use a client side click event (for example with jQuery, to do client side stuff when clicking), you probably better add an event listener to the element like this:

$(document).on('click', '#btnSort', function () {
 // client side stuff here
});

EDIT:

See this for basic client side event binding with jQuery. If this does not look familiar, please read about JavaScript / jQuery basics, it will be worth the time

http://jsfiddle.net/6mYQN/1/

Upvotes: 2

Related Questions