Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

call javascript from .net code

Posted on 2011-02-28
4
Medium Priority
?
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

730 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