Asha
Asha

Reputation: 134

calling a jquery function from asp.net code behind

I need to create a pie chart in bootstrap.. On google search I got a piece of code.. how to call this function from asp.net codebehind? How to give data from database to piechart?

$.plot('#placeholder', data, {
    series: {
        pie: {
            show: true
        }
    }
});

Upvotes: 0

Views: 1795

Answers (2)

MusicLovingIndianGirl
MusicLovingIndianGirl

Reputation: 5947

Try

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

Upvotes: 1

Karl Anderson
Karl Anderson

Reputation: 34846

I recommend the opposite, have your jQuery code call the server-side via ASP.NET AJAX Page Methods, like this:

$(document).ready(function() {
    // For example's sake, this will ask the database for 
    // data upon the DOM being loaded
    $.ajax({
        type: "POST",
        url: "PageName.aspx/GetPieChartData",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            // Plot result data returned from AJAX call to server
            $.plot('#placeholder', result.d, {
                series: {
                    pie: {
                        show: true
                    }
                }
            });
        }
    });
});

Note: You will need to rename PageName.aspx to your actual .aspx page name.


Code-behind:

[WebMethod]
public static string GetPieChartData()
{
    // Go to database and retrieve pie chart data
    string data = GetPieChartDataFromDatabase();

    return data;
}

Note: ASP.NET AJAX Page Methods automatically JSON-encode the returned data, so there is no serialization call needed on the server-side. Also, note ASP.NET AJAX Page Methods must be static, as they have no interaction with the actual page class itself.

Upvotes: 0

Related Questions