1

I have searched how to register a Javascript in the code behind file .cs but still have not understand it and what I have tried does not fire the Javascript.

How can I fire an existing javascript function within the

  protected void Page_Load(object sender, EventArgs e)
   {
   }

I have tried

Page.ClientScript.RegisterStartupScript(Type.GetType, "script", "return myFunction()");

But it says that it has invalid arguments? and a bunch of exceptions when I hover the red underline.

Thank you for your help.

Function

function myFunction() {
            var combo = $find("<%= myClients.ClientID %>");
            //prevent second combo from closing
            cancelDropDownClosing = true;
            //holds the text of all checked items
            var text = "";
            //holds the values of all checked items
            var values = "";
            //get the collection of all items
            var items = combo.get_items();
            //enumerate all items
            for (var i = 0; i < items.get_count(); i++) {
                var item = items.getItem(i);
                //get the checkbox element of the current item
                var chk1 = $get(combo.get_id() + "_i" + i + "_chk1");
                if (chk1.checked) {
                    text += item.get_text() + ",";
                    values += item.get_value() + ",";
                }
            }
            //remove the last comma from the string
            text = removeLastComma(text);
            values = removeLastComma(values);

            if (text.length > 0) {
                //set the text of the combobox
                combo.set_text(text);
            }
            else {
                //all checkboxes are unchecked
                //so reset the controls
                combo.set_text("");
            }
        }

3 Answers 3

4

GetType is a method. Also, your script should be wrapped in script tags. Try this:

Page.ClientScript.RegisterStartupScript(this.GetType(), 
                                        "script", "<script>myFunction();</script>");
Sign up to request clarification or add additional context in comments.

9 Comments

even though the javascript function already has the tags in the front end code?
@Andy, not according to MSDN. Take a look at their examples: msdn.microsoft.com/en-us/library/…. They include the script tags.
When I do this I get object expected error and it shows this <script>myFunction();<
I have posted in the original post
@user710502, I see no glaring issues. I suggest setting a breakpoint and stepping through your code. Has your function been called? Is the error occurring inside your function or as a result of the function not being found? Based on the information I have, my answer is the best I can come up with.
|
2

Try this instead:

Page.ClientScript.RegisterStartupScript(typeof(string), "script", "return myFunction()");

1 Comment

Tried it too it seems like it's not firing it.. not sure why
1

You cannot have a return statement outside a function. Try this hope it helps you provide the function "myFunction" exists in memory before it gets called.

Page.ClientScript.RegisterStartupScript(Type.GetType(), "script", "myFunction()", true);

8 Comments

Where does myFunction lie on the page?
Do you get any js error on page load? Can you see what is rendered on the page in view source?
I think it is not finding the object because it is on the OnLoad, it would have probably not created yet, is there a way to add this on a different stage like when it finishes loading or something?
Are you using jQuery or any other client side library? or else you can use document.body.onload = function(){ myFunction(); }
No i am not using any other libraries, do the document.body.onload, can I add this in the Page.ClientScript....
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.