Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an Enable.js file that has an Enable() function. I want to call this Enable function from the C# codebehind. My .js file and c# file are in same application.

I have tried this, but it doesn't help me.

Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "Enable(true)", true);
share|improve this question
2  
What happens? Is there a JavaScript error? Is Enabled(true) contained in the response HTML? (Remember, that will only happen on the client after the response is received.) – user166390 Feb 22 at 9:46
Look into your browser console and tell us if you got any errors. – ĆukaszW.pl Feb 22 at 9:51
what are you actually doing in the enable.js and are you able to see any error..? – Sravan Feb 22 at 9:59

4 Answers

Try this:

   ScriptManager.RegisterClientScriptInclude(
                this,
                typeof(Page),
                "Enable-js",
                ResolveClientUrl("~/scripts/Enable.js"));


ScriptManager.RegisterStartupScript(
                this, GetType(), Guid.NewGuid().ToString(),
                "Enable(True);", true); 

http://msdn.microsoft.com/pt-br/library/bb337005.aspx

share|improve this answer
1  
I think ScriptManager is used only when Ajax controls like Update Panel are used.This may not help. – Sravan Feb 22 at 10:06
1  
I edited my answer.Please check it. – zeitgeist Feb 22 at 10:10

I wrote a nice little function for calling jquery or just general JS in C# based on some VB I saw a while ago.

public bool runJQueryCode(string message)
{
    ScriptManager requestSM = ScriptManager.GetCurrent(Page);

    if (requestSM != null && requestSM.IsInAsyncPostBack)
    {
        ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), Guid.NewGuid().ToString(), getjQueryCode(message), true);
    }
    else
    {
        Page.ClientScript.RegisterClientScriptBlock(typeof(Page), Guid.NewGuid().ToString(), getjQueryCode(message), true);
    }

    return true;
}

private string getjQueryCode(string jsCodetoRun)
{
    string x = "";

    x += "$(document).ready(function() {";
    x += jsCodetoRun;
    x += " });";

    return x;
}

So you can just call runJqueryCode("alert('hey')");

share|improve this answer

You are making a mistake here:

Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "Enable(true)", true);

The Enable(true) is not possible its literal string true that you're trying to pass as parameter.

You should try this way, so it may help.Its only a sample for understanding

string func = "showSuccessMessage('"+name+"');";
//Pass string as funcion in c#

This Link will explain calling javascript function with parameter from C# Code behind.

this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "xx",   
"<script>test("+x+","+y+");</script>");
share|improve this answer

I could see "click" in your code. Hence, I assume that you need to click some button to call Enable(true) function inside Enable.js file

Follow these below steps:

  1. Reference your Enable.js file inside <head> section like below.

    <script src="Scripts/Enable.js" type="text/javascript"></script>
    
  2. Enable.js file is given below:

    function Enable(var){
        alert(val)
    }
    
  3. To call on Enable() function on Button1's click event:

    protected void Page_Load(object sender, EventArgs e){
        Button1.Attributes.Add("OnClick", "return Enable('true');");
    }
    

Let me know if you need some more help.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.