0

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);
3
  • 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.) Commented Feb 22, 2013 at 9:46
  • Look into your browser console and tell us if you got any errors. Commented Feb 22, 2013 at 9:51
  • what are you actually doing in the enable.js and are you able to see any error..? Commented Feb 22, 2013 at 9:59

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

I think ScriptManager is used only when Ajax controls like Update Panel are used.This may not help.
2

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.

Comments

0

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')");

Comments

0

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>");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.