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);
Deanna
  • 23,876
  • 7
  • 71
  • 156
lakki
  • 127
  • 1
  • 5
  • 11
  • 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.) –  Feb 22 '13 at 09:46
  • Look into your browser console and tell us if you got any errors. – Łukasz W. Feb 22 '13 at 09:51
  • what are you actually doing in the enable.js and are you able to see any error..? – Sravan Kumar Feb 22 '13 at 09:59

4 Answers4

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

RockOnGom
  • 3,893
  • 6
  • 35
  • 53
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.

Rudi Visser
  • 21,350
  • 5
  • 71
  • 97
Ashok kumar
  • 1,593
  • 4
  • 33
  • 68
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')");

Ryan McDonough
  • 9,732
  • 3
  • 55
  • 76
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>");
Community
  • 1
  • 1
Sakthivel
  • 1,890
  • 2
  • 21
  • 47