1

I have a C# class Data, stored in Data.cs, and there I have a simple method PrintName:

    void PrintName(string name)
    {
        Label lblName = new Label();
        lblName.Text = name;
        cph.Controls.Add(lblName);
    }

where cph is a ContentPlaceHolder of the asp.net page, where an instance of Data is created. And I need to pass some data from jQuery client side to C#, and I am trying this code:

function Print() {
  $.ajax({
    type: "POST",
    url: "Data.cs/Data.PrintName",
    data: '{name: "Steven" }',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function () {
        jAlert('Success', 'Alert Dialog');
    },
    failure: function () {
        jAlert('Failure', 'Alert Dialog');
    }
});
}

$(document).ready(function () {
  Print();
}

The problem is that I am trying to call not the asp.net page itself, but a C# class, those instance has access to the page through cph variable. Is it possible to solve this somehow and pass data to the needed instance of the class?

1

1 Answer 1

1

Declare the method PrintName with WebMethod Attribute. Change your Print() Ajax Method's Url to ".aspx/PrintName"

Sign up to request clarification or add additional context in comments.

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.