1

how to call this javascript function from asp.net codebehind pageload..

  <script type="text/javascript">
     function abc() {
                 alert("Hello! I am an alert box!");
     }
  </script>

Is it possible to pass an integer array in to javascript function from asp.net codebehind pageload?

1

4 Answers 4

10

Try below code :

protected void Page_Load(object sender, EventArgs e)
{
    System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "abc", "abc();", true);
}

1. Update > Passing string parameter :

protected void Page_Load(object sender, EventArgs e)
{
    var message = "hi";
    System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "abc", "abc('" + message + "');", true);
}

JavaScript Method with string parameter :

function abc(message) {
    alert(message + ", I am an alert box!");
}

2. Update > Passing string parameter and numeric array to JS method:

protected void Page_Load(object sender, EventArgs e)
{
    int[] numbers = { 10, 20, 30 };

    string serializedNumbers = (new JavaScriptSerializer()).Serialize(numbers);

    var message = "hi";
    System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "abc", "abc('" + message + "', " + serializedNumbers + ");", true);
}

JavaScript Method with string and numeric array parameters:

function abc(message, numbers) {

    alert(message + ", I am an alert box!");
    for (var i = 0; i < numbers.length; i++) {
        alert(numbers[i]);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

But I need to pass parameters into javascript function.. When I use System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "abc", "abc('hai');", true); it is working ... But when i use string s = "Hai"; System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "abc", "abc(s);", true); It's not working....
You can rewrite your function like following function abc(message){alert(message);} to pass message as argument
that's common understanding. Question was how to call JS method from code behind. By the way thanks for the information. I will update it.
I have a function like $(function donutchartcreation(numbers) { alert(numbers[1]); } Here number[1] can not be identified.. Can you plz help me to find the reason?
@SpiderCode yes that is thing of common understanding, that's y write in comment, anyway grate and to the point answer thanks I can not leave it without up vote :)
1

Regular Page

protected void Page_Load(object sender, EventArgs e) 
{
   ClientScript.RegisterStartupScript(GetType(), "abc" + UniqueID, "abc();", true);
}

Ajax Page

You need to use ScriptManager if you use ajax.

protected void Page_Load(object sender, EventArgs e) 
{
   ScriptManager.RegisterStartupScript(this, GetType(), 
     "abc" + UniqueID, "abc();", true);
}

Comments

0

Try

    Dim script As String = String.Format("abc()", "")
    ScriptManager.RegisterClientScriptBlock(Me, GetType(Page), UniqueID, script, True)

Or simply

ClientScript.RegisterStartupScript(GetType(), "abc", "alert('Hello! I am an alert box!')", true);

2 Comments

hi..I use ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "thisScript", "abc()", true); But it is not working...
Try changing abc() to abc
0

You can't call a JavaScript function from the codebehind, but you can return a response that includes JavaScript that invokes the function when the page loads in the browser. Just make sure that your page includes

<script type="text/javascript">
    function abc() {
        alert("Hello! I am an alert box!");
    }
    abc();
</script>

That could be either as part of the ASPX page or you could register it as a script block in the codebehind.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.