Injecting a javascript code into a label control
This is one of the simplest method to call a javascript function from code behind. You need to do the following:
- Create a new website project
- Add a label and button control on your Default.aspx page
- You body part of the markup should now look something like this:
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblJavaScript" runat="server" Text=""></asp:Label>
<asp:Button ID="btnShowDialogue" runat="server" Text="Show Dialogue" />
</div>
</form>
</body>
- Add a javascript function to show a message box as shown below:
<head runat="server">
<title>Calling javascript function from code behind example</title>
<script type="text/javascript">
function showDialogue() {
alert("this dialogue has been invoked through codebehind.");
}
</script>
</head>
- Now double-click on the button and add the following code:
lblJavaScript.Text = "<script type='text/javascript'>showDialogue();</script>";
- Run your project and click the Show Dialogue button
- A message box will be displayed as shown below:

No comments:
Post a Comment