1

I am registering java script to my Asp.net code behind file, which is working fine. Now, I have some update panels on the same page and problem is whenever there is any change in any of the update panel, this script is automatically getting called. Is there any way that I can stop this happening. I can't remove update panels from my page and this script is also a very essential part of the application. In this situation I am just calling a alert (rad alert with set time out functionality) when Save Button is clicked or an Update routine is called while I have few other buttons in update panels and whenver any of the button which is registered to the update panels clicked, the following script is called un-willingly. Anyone's help will really be appreciated.

following is my Page.ClientScript

                string radalertscript = "<script language='javascript'> Sys.Application.add_load(function(sender, e) {var oWnd = radalert('dialogMessage', 400, 140, 'Saved');window.setTimeout(function () { oWnd.Close(); }, 3000);});</script>";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", radalertscript);
Learning Curve
  • 1,449
  • 7
  • 30
  • 60

2 Answers2

2

You can assign empty string to same key radalert to remove the script.

if(some_condition)
    Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", "");

Edit: Based on comments, you can make it simple without using RegisterStartupScript

In code behind

btnSave.Attributes.Add("", "saveButtonFunction();");

In Javascript

<script language='javascript'>

   Sys.Application.add_load(function(sender, e) {
           if(btnSaveClicked){
              var oWnd = radalert('dialogMessage', 400,140, 'Saved');
              window.setTimeout(function () { oWnd.Close(); }, 3000);
              btnSaveClicked = false;
           }
    });

    btnSaveClicked = false;
    function saveButtonFunction(){
       btnSaveClicked = true;
    };    

</script>
Adil
  • 146,340
  • 25
  • 209
  • 204
  • I have already tried that but it doesn't work. Let suppose I have a ButtonA which is in one of the update panel where I am opening a Rad Window to perform some operation so whenever I click on this ButtonA, This alert pops up before rad window opens and than rad window opens on top of this alert. As settimeout is assigned to this alert it automatically close down after 3 seconds and when I close the Rad Window, this alert pops up once again. – Learning Curve Jan 15 '13 at 12:05
  • 1
    As you haev mentioned of running this scrip only when the condition is met so let suppose I define a condition but once the conditions is met and script is registered to Page Life Cycle than any sub-sequent requests or operations on the same page will call this alert itself. – Learning Curve Jan 15 '13 at 12:10
  • You need to set value in java script on if you do not send a call to server. I think you need to add more information about the scenario for show and not showing alert. – Adil Jan 15 '13 at 12:12
  • Let me Thank you first for your quick responses. I am registering this script when Save button is clicked or when there is a call for an Update routine. I already have certain checks in place when to register this script on my page. Now lets suppose I click on Save button which will ultimately register the script and do its job (I mean will call the alert as it suppose to do). Now, As user is on the same page after saving the record and decide to update one section of the page which is in an update panel (not a full post back) and here this alert is getting called itself. – Learning Curve Jan 15 '13 at 12:19
  • Updated my answer have a look to understand, I changed the way to add javacript to make it simple. – Adil Jan 15 '13 at 12:56
1

Thank you very much for your answer Adil. I already have followed the same approach with little difference. I have taken JavaScript out from my code behind file and have registered Sys.Application.add_load event as follow

    Sys.Application.add_load(DisplayRadAlertHandler);
    function DisplayRadAlertHandler() {
        var getMessage = document.getElementById('<%=radAlertDialogHidden.ClientID%>').value;
        if (getMessage != "") {
            document.getElementById('<%=radAlertDialogHidden.ClientID%>').value = "";
            var oWnd = radalert(getMessage, 400, 140, 'Saved');
            window.setTimeout(function () { oWnd.Close(); }, 3000);
        }
    }

Here I am setting my alert message in a hidden input field from code behind file and in the above event handler I am just checking if message is there than reset the hidden field and display the message. Your approach is also right and I have marked your answer but as I am displaying my message from multiple locations (Save button, Update routine etc.) so by assigning value to hidden input field and than resetting in above event handler looks more appropriate. Thanks once again for your help.

Learning Curve
  • 1,449
  • 7
  • 30
  • 60