0

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
        }
    }
});

2 Answers 2

1

Try

protected void myButton(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", 
       "<script type='text/javascript'>placeholder(params...);</script>", false);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.