views:

116

answers:

4

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

A: 

You need to use Register client Script:

I do it in a 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 it's recommended

I mean... you can do this, and it'll work. But for me the only good reason of inserting a script from asp.net page is to get some client side features of server controls.

So for example to get an instance of control:

var myControlId = this.control.ClientID;

But writing entire javascript functions and logic on the server side is not comfortable when you need to change it or debug it (for me it's hardcoded string).

Jarek
+2  A: 

The simple answer is, you can't. The code in the Page_Load method executes on the server, javascript executes on the client.

If what you want to do is add a call to a javascript method, *in the Page_Load* so that once the page is loaded by the browser, the javascript executes, then 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'll need to have an <asp:ScriptManager> element in your markup for it to use (if memory serves, an exception will be thrown if you don't have one). The text "CallMyMethod" is used by the ScriptManager to uniquely identify the script that it injects for you, and the text "myMethod();" is embedded, so you'll end up with an additional script element in your page similar to this:

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

Why would you want to do this? What's the purpose?

Anyway you can do the following, but I DON'T 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 supply with more information of what you want to achieve to get better answer..

Ekaterina
Why wouldn't you recommend it? There are plenty of good reasons for triggering different client-side behaviour depending on something serverside. This is just one way of accomplishing it...
Rob
+1  A: 

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

ClientScript.RegisterStartupScript(Me.GetType(), "hiya", "alert(""hi!"")", True)

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

This should be available from a child control by tacking "Page." onto the beginning of the code above.

cHao