Ana içeriğe atla

Execute JavaScript function from ASP.NET codebehind


From Suprotim Agarwal  blog. 

Calling a JavaScript function from codebehind is quiet simple, yet it confuses a lot of developers. Here's how to do it. Declare a JavaScript function in your code as shown below:
JavaScript
<head runat="server">
    <title>Call JavaScript From CodeBehind</title>
    <script type="text/javascript">
        function alertMe() {
            alert('Hello');
        }
    </script>
</head>
In order to call it from code behind, use the following code in your Page_Load
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!ClientScript.IsStartupScriptRegistered("alert"))
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(),
            "alert", "alertMe();", true);
    }
}
VB.NET
If (Not ClientScript.IsStartupScriptRegistered("alert")) Then
    Page.ClientScript.RegisterStartupScript _  
    (Me.GetType(), "alert", "alertMe();", True)
End If
The Page.ClientScript.RegisterStartupScript() allows you to emit client-side script blocks from code behind. More info here http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerstartupscript.aspx
To call the JavaScript code on a button click, you can add the following code in the Page_Load
btnCall.Attributes.Add("onclick", " return alertMe();");

For more info : 

Calling JavaScript from ASP.NET Master Page and Content Pages - Part I
Calling JavaScript from ASP.NET Master Page and Content Pages - Part II

Yorumlar

Bu blogdaki popüler yayınlar

Get Computer Name with Open Edge

There are three different methot to get computer name. 1. OS-GETENV Function : MESSAGE     OS-GETENV("COMPUTERNAME")           SKIP            OS-GETENV("HOSTNAME")               SKIP VIEW-AS ALERT-BOX INFO BUTTONS OK. 2. Use hostname utility :  (windows only) DEFINE VARIABLE cHost AS CHARACTER  NO-UNDO. INPUT THROUGH hostname NO-ECHO. SET cHost. INPUT CLOSE.   DISPLAY cHost.   3.  Using .NET assemblies ( Also available get Local IP adress ): DEFINE VARIABLE oIPHostEntry AS System.Net.IPHostEntry NO-UNDO. DEFINE VARIABLE oIPAddress   AS System.Net.IPAddress   NO-UNDO. DEFINE VARIABLE cIPAddresses AS CHARACTER              NO-UNDO. DEFINE VARIABLE iCount       AS INTEGER                NO-UNDO INITIAL 1. oIPHostEntry = System.Net.Dns:GetHostEntry(System.Net.Dns:GetHostName()). DO WHILE TRUE:   oIPAddress = CAST(oIPHostEntry:AddressList:GetValue(iCount),"System.Net.IPAddress") NO-ERROR.   IF ERROR-STATUS:NUM-MESSAGES GT 0 THEN     L

Generate pasword with OpenEdge command line genpassword utitiy

The 'genpassword' utility is currently only installed for AppServers and development licenses which contain the AdminServer framework It's call ' bin/genpassword.bat'. java base usefull utility for  Password Encrypter for ubroker.properties.  For more information in Progress Documentation . Open the proenv utitiy  and after then run script. genpassword -password text [ -verify encrypted-password ] c:\dlc\prowin32.exe -db test -1 -U test -P oech1::24373c33

OS-GETENV FUNCTION IN OPENEDGE ABL

Get some operating system environment with OpenEdge Abl language MESSAGE     OS-GETENV("COMPUTERNAME")           SKIP            OS-GETENV("HOSTNAME")               SKIP    OS-GETENV("USERNAME")               SKIP    OS-GETENV("OS")                     SKIP    OS-GETENV("HOMEDRIVE")              SKIP    OS-GETENV("PROCESSOR_ARCHITECTURE") SKIP    OS-GETENV("PROCESSOR_IDENTIFIER")   SKIP    OS-GETENV("PROCESSOR_LEVEL")        SKIP    OS-GETENV("PROCESSOR_REVISION")     SKIP    OS-GETENV("HighestNumaNodeNumber")  SKIP    OS-GETENV("ProgramW6432")           SKIP    OS-GETENV("UserDnsDomain")          SKIP    OS-GETENV("WINDIR")                 SKIP    OS-GETENV("PROMPT")                 SKIP    OS-GETENV("LOCALAPPDATA")           SKIP    OS-GETENV("PATH")                   SKIP    OS-GETENV("TEMP").