0

Currently I have this...

    public void lnkTag_Click(object sender, EventArgs e){
        ...
    }

Which is attached to the click() event of link buttons, but the problem is this requires a form resubmission when the user tries to back in the browser after clicking one.

I'd like to turn this into an ajax call that passes in the text value of the link. So to have a method like this in the code behind:

    public void lnkTag_Click(string linkText){
        ...
    }

Where this method is accessed via ajax on a

  $('myLinkButton').click(function() {
      $.ajax...
  })

Any thoughts? Thanks.

Nick B
  • 657
  • 1
  • 13
  • 33
  • You are using asp.net web forms? If so, you need to decorate your method with `WebMethod` attribute. Check out this [thread](http://stackoverflow.com/questions/10748223/jquery-ajax-error-while-using-c-sharp-function/10748251#10748251), maybe you're facing a similar issue. – Sang Suantak Jul 30 '12 at 18:26
  • @jSang Yeah, I'm using asp.net web forms, k, then what'd be my next step? – Nick B Jul 30 '12 at 18:27
  • @jSang How do I also pass the value of the link button to this method though? – Nick B Jul 30 '12 at 18:28

1 Answers1

2

Do the following for sending the parameter value:

$('myLinkButton').click(function() {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "yourpage.aspx/lnkTag_Click",
        data: "{'linkText': '" + linkTextValue + "'}",
        dataType: "json",
        success: function(data) {
            //do something if it's successful
        },
        error: function(jqXHR, textStatus, errorThrown) {
            //do something if there's an error
        }
    });
});
Sang Suantak
  • 5,213
  • 1
  • 27
  • 46
  • that's great thanks, I guess the problem I'm facing now is since I'm using Sitecore, so I have a .ascx.cs for my code behind, and a .ascx for the front end... which represent a portion of the page I'm currently on... is there a way to access the code behind another way than 'yourpage.aspx/lnkTag_Click' ? – Nick B Jul 30 '12 at 18:46
  • 1
    @barronick [**this**](http://stackoverflow.com/questions/579024/calling-an-ascx-page-method-using-jquery) might help with your query. – Sang Suantak Jul 31 '12 at 04:36