0

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?

5
  • Hello. Sorry, but this "protected void Click_btnSort(object sender, EventArgs e)" isn't JavaScript. What do you want to run? Commented Jun 5, 2013 at 12:09
  • <a id="btnSort" href=# data-role="button" onclick="Click_btnSort()">Sort</a> By the way , is your method definition in JavaScript? Commented Jun 5, 2013 at 12:09
  • @AurelioDeRosa that is ASP.NET and C#. Commented Jun 5, 2013 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? Commented Jun 5, 2013 at 12:12
  • have you tried adding a listener $(document).on('click', '#btnSort', function () { Click_btnSort(); }); ? Commented Jun 5, 2013 at 12:27

3 Answers 3

2

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/

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

Comments

1

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.

Comments

0

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.

Comments

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.