Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

call javascript from .net code

Posted on 2011-02-28
4
Medium Priority
?
327 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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

971 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