• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

call javascript from .net code

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
vbnetcoder
Asked:
vbnetcoder
  • 3
1 Solution
 
Rajkumar GsSoftware EngineerCommented:
<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
 
Rajkumar GsSoftware EngineerCommented:
-- 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
 
vbnetcoderAuthor Commented:
ty
0
 
Rajkumar GsSoftware EngineerCommented:
Glad to help you
Raj
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now