Solved

call javascript from .net code

Posted on 2011-02-28
4
320 Views
Last Modified: 2012-05-11
Is it possible to call .net code from javascript? I want my button click event to launch javascript code contained in my <head> tag.

I there is another way I could better handle this I am open to suggestions
0
Comment
Question by:vbnetcoder
  • 3
4 Comments
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34999369
<script>
function Test()
{
alert('Calling this function in Codebehind')
}
</script>

Open in new window


In order to call Test() in page_load event of codebehind file ,I mean Cs /vb file
Button1.Attributes.Add("onclick", " return Test();");

Open in new window


This will call Javascript from serverside when the corresponding event is fired.
Javascript in codebehind

To Use confirm message directly from codebehind
btnSubmit.Attributes.Add("onclick", "if(!confirm('Are you sure?')) return false;") 

Open in new window

0
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 500 total points
ID: 34999373
-- javascript function

  function alertMe() {

            alert('Hello');

        }

Open in new window



-- code-behind c# code
if (!ClientScript.IsStartupScriptRegistered("alert"))

{

    Page.ClientScript.RegisterStartupScript

        (this.GetType(), "alert", "alertMe();", true);

}

Open in new window



-- code-behind vb code
If (Not ClientScript.IsStartupScriptRegistered("alert")) Then

    Page.ClientScript.RegisterStartupScript _

    (Me.GetType(), "alert", "alertMe();", True)

End If

Open in new window

0
 

Author Closing Comment

by:vbnetcoder
ID: 35000840
ty
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 35003856
Glad to help you
Raj
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now