Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
Hello. Sorry, but this "protected void Click_btnSort(object sender, EventArgs e)" isn't JavaScript. What do you want to run? – Aurelio De Rosa Jun 5 at 12:09
<a id="btnSort" href=# data-role="button" onclick="Click_btnSort()">Sort</a> By the way , is your method definition in JavaScript? – harsha Jun 5 at 12:09
@AurelioDeRosa that is ASP.NET and C#. – MrCode Jun 5 at 12:11
The method is from the page's code-behind isn't JavaScript. I'm expecting that because I've used runat="server" I'll be able to handle the event with the c# code-behind. Am I wrong here? – Anton Belev Jun 5 at 12:12
have you tried adding a listener $(document).on('click', '#btnSort', function () { Click_btnSort(); }); ? – Omar Jun 5 at 12:27

2 Answers

up vote 1 down vote accepted

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/

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.