javascript from code behind or button

I'm using some code I found here to be able to reorder a list in asp.net. Users pick items and add to list then reorder them.

Everything works great but I need to change it for my situation. Currently it doesn't reorder my list in the DB until an item is dragged and resorted.

I want to be able to add a "save" button that will force this script to run and reorder everything even if the user never reorders it themselves manually by dragging.

I googled like crazy how to run javascript from codebehind or a button and found hundreds of examples but none of them work for me. I'm 99% sure that it's me who is the problem here, not the solutions I've tried so I don't know where else to turn to except here.

Here is the javascript.

<script type="text/javascript">

         $(function () {
             $('#sortable').sortable({
                 placeholder: 'highlight',
                 update: OnSortableUpdate
             });
             $('#sortable').disableSelection();

             var progressMessage = 'Saving changes... <img src="loading.gif"/>';
             var successMessage = 'Saved successfully!';
             var errorMessage = 'There was some error in processing your request';
             var messageContainer = $('#message').find('p');

             function OnSortableUpdate(event, ui) {
                 var order = $('#sortable').sortable('toArray').join(',').replace(/id_/gi, '')
                 //console.info(order);

                 messageContainer.html(progressMessage);

                 $.ajax({
                     type: 'POST',
                     url: 'Sortable.asmx/UpdateItemsOrder',
                     data: '{itemOrder: \'' + order + '\'}',
                     contentType: 'application/json; charset=utf-8',
                     dataType: 'json',
                     success: OnSortableUpdateSuccess,
                     error: OnSortableUpdateError
                 });
             }

             function OnSortableUpdateSuccess(response) {
                 if (response != null && response.d != null) {
                     var data = response.d;
                     if (data == true) {
                         messageContainer.html(successMessage);
                     }
                     else {
                         messageContainer.html(errorMessage);
                     }
                 }
             }

             function OnSortableUpdateError(xhr, ajaxOptions, thrownError) {
                 messageContainer.html(errorMessage);
             }

         });

    </script>

Open in new window

someonenamedbrendanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
The code you are showing looks like jQuery to me.  To use jQuery code, you must load the jQuery library first.  And that has to be done in the page that is running the javascript because all that is done in the browser, not on the server.
0
someonenamedbrendanAuthor Commented:
I just snipped the script I need run out of the .aspx page. The script runs fine and is doing everything it's supposed to do. I just want to be able to run it on command... like via a button on the page or in code behind.

But I guess I should change my question to how to run this jquery from code behind.
0
Dave BaldwinFixer of ProblemsCommented:
As far as I know, to run it "on command" it would have to be a 'named' function that can be called by that name.  Then you could put 'onclick()' in an element like a button on that page.  What you are showing is a 'generic' function that runs when the web browser encounters it on page load.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Julian HansenCommented:
Try adding this
  $('#sortable').on('sortupdate', OnSortableUpdate);
	
  $('#save').click(function() {
    $('#sortable').trigger('sortupdate');
  });

Open in new window

0
someonenamedbrendanAuthor Commented:
either i'm using that incorrectly or it didnt work and broke the drag and drop. :(
0
Julian HansenCommented:
Can you post your code - I managed to get it to work see below
<!doctype html>
<html>
<head>
<title>Test</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery.js"></script>
<script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
<script type="text/javascript">
$(function() {
  $('#sortable').sortable({
    placeholder: 'highlight',
    update: OnSortableUpdate
  });
  
  $('#sortable').on('sortupdate', OnSortableUpdate);
  
  $('#save').click(function() {
    $('#sortable').trigger('sortupdate');
  });
  
  $('#sortable').disableSelection();

  var progressMessage = 'Saving changes... />';
  var successMessage = 'Saved successfully!';
  var errorMessage = 'There was some error in processing your request';
  var messageContainer = $('#message').find('p');

  function OnSortableUpdate(event, ui) 
  {
    var order = $('#sortable').sortable('toArray').join(',').replace(/id_/gi, '')
    //console.info(order);

    messageContainer.html(progressMessage);

    $.ajax({
      type: 'POST',
      url: 'e002.php',
      data: '{itemOrder: \'' + order + '\'}',
      contentType: 'application/json; charset=utf-8',
      dataType: 'json',
      success: OnSortableUpdateSuccess,
      error: OnSortableUpdateError
    });
  }

  function OnSortableUpdateSuccess(response) 
  {
    if (response != null && response.d != null) {
      var data = response.d;
      if (data == true) {
        messageContainer.html(successMessage);
      }
      else {
        messageContainer.html(errorMessage);
      }
    }
  }

  function OnSortableUpdateError(xhr, ajaxOptions, thrownError) {
    messageContainer.html(errorMessage);
  }
});
</script>
<style type="text/css">
</style>
</head>
<body>
<div class="ui-widget" id="message">
  <div class="ui-state-highlight ui-corner-all" style="margin-top: 20px; padding: 0 .7em;">
    <p>
      Reorder Items
    </p>
  </div>
</div>
<ul id="sortable">
  <li class="ui-state-default" id="a_1"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 1</li>
  <li class="ui-state-default" id="a_2"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 2</li>
  <li class="ui-state-default" id="a_3"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 3</li>
  <li class="ui-state-default" id="a_4"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 4</li>
  <li class="ui-state-default" id="a_5"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 5</li>
  <li class="ui-state-default" id="a_6"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 6</li>
  <li class="ui-state-default" id="a_7"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 7</li>
</ul>
<input type="button" value="Save" id="save"/>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
someonenamedbrendanAuthor Commented:
I tried a bunch of different things and kept getting errors in the javascript debugger. "uncaught typeerror" for example. So, for fun a removed all my jquery references:
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>

    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.8/jquery-ui.min.js" type="text/javascript"></script>


     <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
     <script src="../Scripts/jquery.ui.touch-punch.min.js"></script>

Open in new window


and replaced them with yours and the errors went away and the save button works.

Thanks.
0
Julian HansenCommented:
You are welcome - thanks for the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.