1

I have read many reference for this question but not getting result. So, I thought let me try to explain my scienario here.

This is my 3 script which is in view.ascx control. I want to call it in .cs file function and in click event.

view.ascx

<script type="text/javascript">
function startLoading() {
    alert("load start");
    var el = jQuery(".scp-entry-header");
    App.blockUI(el);
}


function stopLoading() {
    alert("load end");
    var el = jQuery(".scp-entry-header");
    App.unblockUI(el);
}

function getCalendarData() {
    alert("calendarcalled");
}
</script>

view.ascx.cs

//ViewData button click
protected void btnGetData_Click(object sender, EventArgs e)
{
    //How to call startLoading function here?
    process from database;
    //How to call stopLoading function here?
}

//simple function call
protected void showCalendar()
{
    //How to call getCalendarData function here?
}

I have read about RegisterScript but not getting result. I don't understand where to RegisterScript and how to call it in function.

Can anybody please suggest me in detail how can I call javascript function here?

2
  • Possible duplicate of How do I call javascript from asp.net button click? Commented Jan 11, 2017 at 12:23
  • @rcdmk it's not same. I want to call javascript function from .cs file(not on button click only). I also want to call javascript in simple function of .cs file. I have edited my question to more clear it. Please correct me to stop misunderstanding. Commented Jan 11, 2017 at 12:28

2 Answers 2

1

Try This

 protected void btnGetData_Click(object sender, EventArgs e)
 {
        //How to call startLoading function here?
        process from database;
        //How to call stopLoading function here?
       Page.ClientScript.RegisterStartupScript(this.GetType(), "script", "startLoading", true);
 }  

or

 ScriptManager.RegisterStartupScript(Page, Page.GetType(), "key", "<script>alert('Hello.')</script>", false);
Sign up to request clarification or add additional context in comments.

2 Comments

Will this helpful in case of User Control? My question is for UserControl
You need to add delegation or custom event for link button of you user control and using custom event you can call this .
1

Have in mind that you can't call a JavaScript function in the middle of your C# code and continue executing you server side code.

All calls to a JavaScript function on the .cs file are just a scheduling/marking for execution after the server side code executes. They are not executed alongside server code.

In other words, you will have to implement this with UpdatePannels and async code for this to work, or better yet, implement this as async calls in JavaScript.

Code for the client should be on the client. Try not to mix them.

1 Comment

Thanks. I have read some reference(stackoverflow.com/questions/18036508/…) but it is not useful. Can you guide me what should I do to handle above situation?

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.