user3395635
user3395635

Reputation: 17

How to call javascript function in code behind .cs file in Asp.net

I tried to call the function ApplyCSS() which is in my SearchPage.ascx page inside the Script tag and in .CS file I am trying to call that function using the below code:

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Script", "ApplyCSS();", true);

but I am getting javascript runtime error object expected.

Upvotes: 0

Views: 13619

Answers (3)

Vikram
Vikram

Reputation: 1627

   <script type="text/javascript">
        function MyFunc(){
};
</script>


 ScriptManager.RegisterStartupScript(this, Page.GetType(), "key", "MyFunc()", true);

Semicolon is not required after MyFunc() Call. You can refer this link

Upvotes: 1

user3559224
user3559224

Reputation:

Have your script line is should be

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

and please check your function is does not threw any error.

and try with this

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Script", "javascript:ApplyCSS();", true);

Upvotes: 0

Erwin
Erwin

Reputation: 1502

The likely cause of this error is that ApplyCSS(); isn't defined at the time when the function is called.

So, is the ApplyCSS(); function defined in a .js file? If so, you have to go with this kind of approach:

$(document).ready(function () { ApplyCSS(); } );

You'll need jQuery to do this

Upvotes: 2

Related Questions