I wrote a javascript with a asp.net page.
In Asp.net Page
<HTML> <HEAD> <script type="text/javascript"> function Myfunction(){ document.getElementId('MyText').value="hi"; } </script> </HEAD> <BODY> <input type="text" id="MyText" runat="server" /> </BODY>
In Code-behind
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Session("My")= "Hi" Then I want to call "Myfunction" javascript function End If End Sub
How can I do?
What you can do to call a method from server using JavaScript is. Use WebMethod as attribute in target methods. Add ScriptManager setting EnablePageMethods as true . Add JavaScript code to call the methods through the object PageMethods .
The JavaScript Client Side function will be called from Code Behind (Server Side) using ClientScript RegisterStartupScript method. The following HTML Markup consists of an ASP.Net Button and a Label control.
Solution 2Both RegisterClientScript and RegisterStartupScript use to add javascript code in web form between <form runat="server"> tag and </form> tag. RegisterClientScript adds javascript code just after <form runat="server"> tag while RegisterStartupScript adds javascript code just before </form> tag.
One way of doing it is to use the ClientScriptManager
:
Page.ClientScript.RegisterStartupScript( GetType(), "MyKey", "Myfunction();", true);
This is a way to invoke one or more JavaScript methods from the code behind. By using Script Manager we can call the methods in sequence. Consider the below code for example.
ScriptManager.RegisterStartupScript(this, typeof(Page), "UpdateMsg", "$(document).ready(function(){EnableControls(); alert('Overrides successfully Updated.'); DisableControls();});", true);
In this first method EnableControls() is invoked. Next the alert will be displayed. Next the DisableControls() method will be invoked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With