Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to disable JQuery tabs programmatically. The tabs are inside an update panel (Ajax) and the update panel is in an ASP.NET page. Code:

<link type="text/css" rel="stylesheet" href="http://ui.jquery.com/testing/themes/base/ui.all.css" />

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>

<script type="text/javascript" src="http://ui.jquery.com/testing/ui/ui.core.js"></script>

<script type="text/javascript" src="http://ui.jquery.com/testing/ui/ui.tabs.js"></script>

<script type="text/javascript">

    $(document).ready(function(){
        $("#example").tabs();
    });


    function hidetabs(){
        $(document).ready(function(){
        $("#example").tabs();
        $('#example').data('disabled.tabs', [1, 2]);});
    }
</script>


<%@ Page Language="C#" MasterPageFile="~/any.Master" AutoEventWireup="true" Codebehind="anycode.aspx.cs"
    Inherits="anycode" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

<asp:UpdatePanel ID="UpdatePanel_Deal_Import" runat="server">
<ContentTemplate>
<div id="example">
<ul>
<li><a href="#fragment-1"><span>One</span></a></li>
<li><a href="#fragment-2"><span>Two</span></a></li>
<li><a href="#fragment-3"><span>Three</span></a></li>
</ul>
<div id="fragment-1">
<p>
First tab is active by default:</p>
<pre><code>$('#example').tabs();</code></pre>

<asp:Button ID="btn_Save" OnClick="btn_Save_Click" runat="server" Text="Save" Visible="False" CommandName="Save">
</div>

<div id="fragment-2">
Lorem ipsum dolor sit amet, consectetuer 
ut laoreet dolore magna aliquam erat volutpat.
</div>
<div id="fragment-3">
Lorem ipsum dolor sit amet, consectetuer 
aliquam erat volutpat.
</div>
</div>

      </ContentTemplate>
</asp:UpdatePanel>
</asp:Content>


Code behind:

protected void btn_Save_Click(object sender, EventArgs e)
{
//here I need to diable the panels.

}

The function btn_Save_Click doesn’t post the page so it doesn’t call the Javascript/jquery hidetabs function. Thanks for any help!!!

like image 820
Ixtlan Avatar asked Feb 16 '09 23:02

Ixtlan


People also ask

Can we call C# code behind using jquery?

Can we call C# code behind using jQuery? we can but we need to specify the backend method as static . Yes,But it having some prerequisiteC# method mark as webmethod. Webmethod should be public.

How call JavaScript code behind method in asp net?

The Code Behind function can be called from Client Side (JavaScript) using ASP.Net AJAX PageMethods in ASP.Net. The first thing you need to do is add a ASP.Net AJAX ScriptManager to the page and set its EnablePageMethods property to true.


1 Answers

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

like image 139
hamed aj Avatar answered Sep 29 '22 11:09

hamed aj