How to call a function in asp.net using javascript?

I am using jquery tablednd plugin

$(document).ready(function () { $('#mygridview').tableDnD(); }) 

so that my rows in my gridview are draggable. When I dragged a line, I want to call a function in asp.net to preserve the position of the lines. But I can’t find a suitable event in gridview that only responds when a row is clicked.

There is an onDrop method in tableDnd.

 $(document).ready(function () { $('#mygridview').tableDnD({ onDrop: function(table, row) { }); }) 

But how could I call the asp.net method from there? I read some posts about ajax, but I do not understand this.

And I also read about using PageMethods , but it did not call my function.

The question is, how can I write something that updates the table in my database?

UPDATED:

I solved this with the IPostBackEventHandler method.

I had to extend both my user control and my page using IPostBackEventHandler and then added the public void RaisePostBackEvent (string eventArgument) function to both the user control and the page. And finally:

 onDrop: function (table, row) { __doPostBack('<%= this.UniqueID %>', 'Select'); } 

If someone has a problem with onDrop, you need to give an identifier to each line as follows:

  var i = 0; $("#<%= gridviewID.ClientID %>").find('tr').each(function () { $(this).attr('id', i++); }); 

above the start of tablednd.

Thanks!

0
source share
1 answer

In general, there are two ways: one using AJAX and the second using postbacks.

AJAX Method:

  • Add a ScriptManager to the page, for example:

     <asp:ScriptManager runat="server" ID="ScriptManager1" EnablePageMethods="true" EnablePartialRendering="true" /> 
  • Make the server-side method invoked as public static. Also mark it with the System.Web.Services.WebMethod attribute, for example:

     [WebMethod] public static string DoServerStuff(string parameter) { return "Parameter passed: " + parameter; } 
  • Call it from the client side through the PageMethods class, for example:

     function doClientStuff() { var result = PageMethods.DoServerStuff('test parameter'); alert(result); } 
  • To do the same using jQuery, check this out.


Postback Method:

  • Make a page (which contains the method that will be called) to implement the IPostBackEventHandler interface.
  • Call the __doPostback method on the client side, for example:

     function doClientStuff() { var result = __doPostBack('<%= this.UniqueID %>', 'Select'); } 
  • Embed server-side logic inside the IPostBackEventHandler.RaisePostBackEvent method of the page.

  • Read more about raising client-side feedback here .

+2
source

Source: https://habr.com/ru/post/983253/


All Articles