calling javascript method from asp.net

Last post 11-27-2007 10:43 AM by aakbar. 17 replies.

Sort Posts:

  • Re: calling javascript method from asp.net

    11-16-2007, 1:05 PM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 8,724

    As I have already told you, you can not do what you are trying to do because inline script will not execute that way. Inline script only executes on a complete page load/reload.

    NC...

  • Re: calling javascript method from asp.net

    11-27-2007, 7:43 AM
    • Loading...
    • zeemalik78
    • Joined on 12-26-2006, 10:34 AM
    • Pakistan
    • Posts 62

    If you are using AJAX then the only way i have found yet to give an alert to a user on return to the Asynchronous post back is to add an "end request" handler to the PageRequestManager.

    In this way you can tell the request manager to run a javascript function on returning from a Asynchronous post back event of AJAX.

    Code for doing this is :

    function load()

    {
       Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    }

    where "EndRequestHandler" will be the name of your javascript function you want to call.
    Call the above function in Onload event of <body> tag:

    <body onload="load()">

    function EndRequestHandler()

    {

              alert("You record has been saved successfully");

    }

    Now If you want to give a different message based on your logic in server side code (code behind) then you can use a server side Hidden Field:

    <input id="hdnValue" type="hidden" runat="server"  value="" />

    Set its value in server side code on Asychronous Post Back: 

    Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateSample.Click

        If condition Then

          hdnValue.value = "do this"

        Else

          hdnValue.value = "do that"

        End If  End Sub

    Now you can check the value of this Hidden Field in your Client Side EndRequestHandler function and give a different alert to user based on its value:

    function EndRequestHandler()

    {

         if (document.getElementById('<%= hdnValue.ClientID %>').value == "do this")

         {

              alert("You record has been saved successfully");

         }

         else

         {

              alert("There is an error");

         }

     }

     
  • Re: calling javascript method from asp.net

    11-27-2007, 10:43 AM
    Answer
    • Loading...
    • aakbar
    • Joined on 10-26-2007, 6:49 AM
    • Posts 23

    thanks all of you for your contributions.

    yes finally i have found the solution n now it seems very simple and easy Smile

    so far we were talking about ClientScriptManager provided by System.Web.UI namespace and tried use its registerScript methods to work out.

    but i have used System.Web.UI.ScriptManager provided by Ajax extenstions (http://asp.net/AJAX/Documentation/Live/mref/T_System_Web_UI_ScriptManager.aspx)

    and this class provided you to register your script just for an updatePanel which is reloaded. so put you button(on whose event you want to initiate the process) inside an update panel and inside its event handler register script using ScriptManager not ClientScriptManager. it is very important to know this difference as these are two separate classes. have a look at the following code.

    Protected Sub btnCreateSample_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateSample.Click

    Dim strMessage As StringBuilder = New StringBuilder()

    If ctrlSample.validate() Then

    Dim newSample As Sample = ctrlSample.exportSample()

    If Sample.addSample(newSample) Then

    strMessage.Append("alert('Sample added successfuly');")ScriptManager.RegisterStartupScript(btnCreateSample, btnCreateSample.GetType(), "success", strMessage.ToString(), True)

    ctrlSample.refreshControl()

    Else

    strMessage.Append("alert('Error while adding sample try later');")

    ScriptManager.RegisterStartupScript(btnCreateSample, btnCreateSample.GetType, "failure", strMessage.ToString(), True)

    End If

    Else

    strMessage.Append("alert('Please enter correct values');")

    ScriptManager.RegisterStartupScript(btnCreateSample, btnCreateSample.GetType, "input", strMessage.ToString(), True)

    End If

    End Sub

    so using this RegisterStartupScript method we could call a java script method on the reload of just an update panel. while using the RegisterStartupScript of ClientScriptManager requires the whole page to be reloaded.

    thanks

    WE ALL LEARN BY SHARING KNOWLEDGE
    ALI AKBAR
Page 2 of 2 (18 items) < Previous 1 2
Microsoft Communities
Page view counter