2

I have an HTML page and an .aspx page in my project. I defined a javascript function in the html page and trying to call it from code behind of the .aspx page

I tried using ClientScript.RegisterStartupScript() and ScriptManager.RegisterStartupScript() as following.

HTML page - MainPage.html

<script type="text/javascript">
    $(document).ready(function () {
        function functionName() {
            #content
        };
    });
 </script>

.aspx page - form.aspx.cs

Try 1

protected void Button2_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, GetType(), "MainPage.html", "functionName();", true);
}

Try 2

protected void Button2_Click(object sender, EventArgs e)
{
    ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:functionName(); ", true);
}

I am trying to call function functionName() from HTML page when Button2 from aspx page was clicked

user147504
  • 145
  • 1
  • 14
  • 1
    Have you tried declaring `functionname` outside of the `ready` function? – SBFrancies Jul 11 '19 at 15:39
  • check this https://stackoverflow.com/questions/14578911/call-javascript-function-in-aspx-on-aspx-cs-using-a-button – Hitesh Anshani Jul 11 '19 at 15:44
  • @SBFrancies Just tried now. When I debug the application, it is saying the function functionName() was not defined in the aspx page – user147504 Jul 11 '19 at 15:48
  • @HiteshAnshani In the link you shared, javascript function is called in the same page but in my case a function from different html page is being called in this aspx.cs page – user147504 Jul 11 '19 at 15:52
  • Why does it have to be called on the server, can you not have it on the submit button waiting as a listener for when it is hit? – Greg Jul 11 '19 at 16:00
  • @Greg I am trying to make some changes on HTML page when button on aspx page is clicked. Is there any other better way to do that? – user147504 Jul 11 '19 at 16:16
  • JavaScript will only edit on the client, as soon as the PostBack executes for the server your changes will be erased once the server renders the new page. So the question would be what functionality are you trying to achieve by changing to do what? – Greg Jul 11 '19 at 17:52

1 Answers1

0

I don't have rights to comment your question to ask for clarification, so I'm going out on a limb, here. Apologies if I miss something.

Would you be able to put the jQuery code from your extract in a js script file, and include it both in your Html page and in your aspx page?

Then, you could manage the button click in your aspx page on the client side, without sending it back to the server (if your button click doesn't do anything on the server, other than registering the js code)

Something like this:

<script type="text/javascript">
    $(document).ready(function () {
        $('#yourButtonId').on('click', function (e) {
            e.preventDefault(); // this prevents the form to be submitted to the server
            functionName(); // Execute your function
        });
    });
 </script>
Damien7792
  • 85
  • 1
  • 6