Calling javascript function from Page_Load in encoding

How can I call the javascript function that is in the aspx page from the Page_Load method in code?

+2


a source to share


8 answers


The easiest way is to use the ClientScript property on the page. You can register some code to run when the page is loaded using something like

ClientScript.RegisterStartupScript(GetType(), "hiya", "alert('hi!')", true);

      



See http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx for details .

This should be accessible from the child control by clicking Page. at the beginning of the code above.

+3


a source


Simple answer: you can't. The code in the Page_Load method is executed on the server, the javascript is executed on the client.

If you want to add a javascript method call to Page_Load so that after the page is loaded by the browser, the javascript is executed, you can use the ScriptManager:

if (myConditionForAddingCallToJavascriptIsMet)
{
    Page.ClientScript.RegisterClientScriptBlock(typeof(ScriptManager), "CallMyMethod", "myMethod();");
}
else
{
    // Do something else, add a different block of javascript, or do nothing!
}

      



To use this, you need an element <asp:ScriptManager>

in your markup to use it (if memory is used, an exception will be thrown if you don't have one). The "CallMyMethod" text is used by the ScriptManager to uniquely identify the script it enters for you and the "myMethod ();" is inlined, so an extra script element similar to this will be added to your page:

<script language="javascript" type="text/javascript">
    myMethod();
</script>

      

+5


a source


Why do you need this? What's the goal?

You can do the following anyway, but I DO NOT recommend it !!! :

     protected void Page_Load(object sender, EventArgs e)
    {
         string pagename = "Test.aspx"; 
         String scriptString = "<script language=JavaScript> function DoClick() {"; 
         scriptString += " window.showModalDialog('" + pagename + "' )}"; 
         scriptString += "</script>";            

if(!this.IsStartupScriptRegistered("Startup")) //This is **not** a good practice
this.RegisterStartupScript("Startup", scriptString); 
    }

      

Can you please provide more information on what you want to achieve in order to get a better answer.

+2


a source


Shide's solution works. The problem is that you want, for example, to run your piece of code only after a postback. The solution I found is to use "cookie": I check if cookie x exists if it doesn't run the javascript code and doesn't set the cookie. When you hit refresh, the cookie will be found, so the code snippet will not run.

function pageLoad() {
   if(localStorage.getItem("cookie") === null) {
      localStorage.setItem("cookie", true);

      //run code
   }
}

      

Any code that needs to be run every time just leaves it outside the if statement.

+1


a source


You need a Register Script client:

I do this in the page load method:

if (!this.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "HelpScript"))
{
  var scriptSource = "function ShowHelp() { alert(""); };\n";
  ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "HelpScript", scriptSource, true);
}

      

but I don't think he recommended

I mean ... you can do it and it will work. But for me, the only good reason to insert a script page from asp.net is to get some server-side system functionality on the client side.

So, for example, to get an instance of a control:

var myControlId = this.control.ClientID;

      

But writing entire javascript functions and server side logic is awkward when you need to modify or debug it (for me it's a hardcoded string).

0


a source


If you just want to call a JavaScript function (and don't need to pass anything from your code, then use this ...)

  • document.onload = function () {your-function (); };
  • window.onload = function () {your-function (); };
  • document.readyState = function () {your-function (); }

    ... depending on your specific requirements.

Hope this makes sense to you and answers your question.

0


a source


This is a little trick I found without server side code. this function will be called when the page is loaded (can be used with refresh panel):

<script>
function pageLoad() {
        //Do your stuff
        }
</script>

      

he is unknown and still works like a charm

0


a source


Please try this code:

  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "MyFun1", "test();", true);

      

0


a source







All Articles