Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to be able to call a jQuery JavaScript program from C# program.

How do I do this?
C#
Progress = 123;
ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:setProgress(" + Progress + "); ", true);

JavaScript
<script type="text/javascript">
    $(document).ready(function () {
        function setProgress(progress) {
            var somevalue = process;
            $("#<%= TextBox2.ClientID %>").text(somevalue);

        });
    });
</script>
Posted
Comments
Samatha Reddy G 9-Oct-14 5:29am    
can you please put your .aspx page code

At a glance it looks like the javascript requires slight modification:
JavaScript
//function moved out of jQuery ready wrapper
function setProgress(progress) {
  //var somevalue = process; //process used my mistake - syntax error
  var somevalue = progress; 
  $("#<%= TextBox2.ClientID %>").text(somevalue);
}

Funny (wierd) on the process and progress, I get those mixed up sometimes.
 
Share this answer
 
to call java script you can use RegisterStartUpScript, RegisterClientScript and Client-Side Script

you can find this link useful
Use & Call RegisterStartUpScript, RegisterClientScript and Client-Side Script[^]

your java script function can be put in external js file or in aspx page
 
Share this answer
 
C#
Page page = HttpContext.Current.CurrentHandler as Page;
     page.ClientScript.RegisterStartupScript(typeof(Page), "Test", "<script type='text/javascript'>functionname1(" + arg1+ ",'" + arg2+ "');</script>");
 
Share this answer
 
Your js function into a separate file,then reference the file in asp file
 
Share this answer
 
try like this it is working
write this in .aspx

XML
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function setProgress(test) {
            alert(test);
            return false;
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button Text="text" runat="server" ID="btnTest" OnClick="btnTest_Click" />
    </div>
    </form>
</body>



Write this in .cs

C#
protected void btnTest_Click(object sender, EventArgs e)
        {            
           string Progress = "123";

            ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:setProgress(" + Progress + "); ", true);

        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900