Call JQuery function from code behind (aside) ASP.Net and C# Call JQuery function from code behind (aside) ASP.Net and C# ajax ajax

Call JQuery function from code behind (aside) ASP.Net and C#


I've use following way and for me work 100% properly:

the first i create a function and write my jquery function in to the function in the my page:

<script>    function myFunction(params) {        $.my_jquery(params...)        ......    }</script>

then i used this code in event handler of my control(for example click a button) who my control is inside a update panel:

protected void myButton(object sender, EventArgs e){    .....    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>myFunction(params...);</script>", false);}

successful


Sounds like you need a button that has a client-side action instead of one that posts back.

<asp:Button ID="Clickable" runat="server" text="Click me" OnClientClick="JavascriptCall();" />

Additionally, while jQuery is generally backward compatible, it's not a good idea to reference jQuery's latest .js file directly. Instead I would download a version of it that you want, and place it statically on your site for direct reference of a known version. It is not good to add a dependency on the state or availability of a resource on an external site when not necessary.


Thanks Chris,

Actually yes, part of the solution is to call directly the javascript function, but instead of using a client-side control I called the Javascript once my UpdatePanel request is over as is explained in the following blog:

http://blog.jeromeparadis.com/archive/2007/03/01/1501.aspx

Now my code looks like:

<link type="text/css" rel="stylesheet" href="css/ui.all.css" />    <script type="text/javascript" src="jquery-latest.js"></script>    <script type="text/javascript" src="ui.core.js"></script>    <script type="text/javascript" src="ui.tabs.js"></script>    <script type="text/javascript">    //enable tabs if a deal is selcted or saved.    function EndRequestHandler(sender, args) {        var rec_id = document.getElementById('<%=hidden_value.UniqueID %>').value;        if (rec_id=="")            hidetabs();        else            showtabs();    }    hidetabs();    $(document).ready(function(){        $("#rec_entry").tabs();    });    function hidetabs(){        $(document).ready(function(){        $("#rec_entry").tabs();        $('#rec_entry').data('disabled.tabs', [1, 2, 3, 4, 5]);});    }    function showtabs(){        $(document).ready(function(){        $("#rec_entry").tabs();        $('#rec_entry').data('disabled.tabs', []);});    }    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);    </script>    ...html code add the tabs... 

Code behind:

protected void btn_Save_Click(object sender, EventArgs e) {

        .... code to save the new record ........          UpdatePanel_mypanel.Update();    }

after the panel is updated the EndRequestHandler evalues a flag (in this case a hiddenfield) and calls the Javascript function that enable or disable the tabs.

The endrequesthandler is controlled thanks to this sentence included in my javascript:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

I followed your advice of including the javascript files into my project. Thanks again!

Ixtlan