title: How to programmatically change the OnClientClick event and call it?
body: <p>I have a ListView that contains playlists.  If the user wants to edit an album, they click the edit link and it allows them to remove or add songs to the playlist.  If the user wants to Save a new playlist while keeping the original one, the would click a Save As Button.  Here is what should happen:</p>
  
  <p>If the user clicks Save As and they have not changed the name, I want to display an alert telling them they must change the name.</p>
  
  <p>If the user clicks the Save As and they have changed the name, I want to display an a confirmation that they actually want to save it.</p>
  
  <p>Currently, what is happening is that if I put something like the following in my code behind, the script does not register until the second click and whatever script was registered stays registered meaning that if I end up changing the name and the alert script was registered before, it will display the alert instead of the confirmation.  Here is the code:</p>
  
  <pre><code>if (newname == oldname)
  {
      btnSaveAs.OnClientClick =
    "javascript:alert('Save As requires you to change the name of the playlist. Please
     change the name and try again.');";
  }
  else
  {
      btnSaveAs.OnClientClick = "javascript:confirm('Are you sure you want to save the
      playlist" + newname + "?');";
  }
  </code></pre>
  
  <p>I also tried adding the return false so it would not do a postback, but if I do that, then it does not doesn't actually do anything when I click OK on the confirmation.</p>