0

i have this aspx view :

 jQuery('#addjob').click(function () {
            if ($('#metier').val().length > 0) {
                $('.taglist').append('<li style="line-height: 20px"><a href="" style="width:250px" >' + $('#metier').val() + '<span class="icon-remove"></span></a></li>');
                $.ajax({
                    type: "POST",
                    url: "JobsEdition.aspx/AjouterMetier",
                    data: "{'job': '" + $('#metier').val() + "'}",
                    success: function (msg) {
                        AjaxSucceeded(msg);
                    },
                    error: AjaxFailed
                });
                                              }
            $('#metier').val('');
        });

This is the result i got:

im2

In the code behin i put this method :

 public partial class JobsEdition : System.Web.UI.Page
    {
      List<string> jobs = new List<string>();
        [WebMethod()]
        public void AjouterMetier(string job)
        {
            if (job != "")
            {
                jobs.Add(job);
                Session["jobs"] = jobs;
            }

        }
}

I'm beginner in the use of ajax in Asp.net, so i had always the session variables Session["jobs"] null and the method AjouterMetier was never reached .

  • What is the error that I commited?
  • How can i fix it?
3
  • 1
    if you just type the url <myserver>/JobsEdition.aspx/AjouterMetier (where <myserver> is the url to the server your page is on) into your browser's address bar, do you get any ASPX or HTTP error? Commented May 4, 2014 at 18:27
  • 1
    A WebMethod need to be static. public static void AjouterMetier(string job) Commented May 4, 2014 at 18:30
  • @Samuel when i change it to static ==> i have to change the Jobs list also to static and i got this error session is unknown type Commented May 4, 2014 at 18:42

2 Answers 2

1

To Webmethod work with a session you need anotate this way [WebMethod(enableSession: true)], Who your Webmethod is static you will need to change List to Static too, To acess a session in in a static method you need to refer a session how HttpContext.Current.Session.

I thik this will solve your problem

Try this

cs

static List<string> jobs = new List<string>();
[WebMethod(enableSession: true)]
public static void AjouterMetier(string job)
{

    if (job != "")
    {
        jobs.Add(job);
        HttpContext.Current.Session["jobs"] = jobs;
    }

}

js

jQuery('#addjob').click(function () {
            if ($('#metier').val().length > 0) {
                $('.taglist').append('<li style="line-height: 20px"><a href="" style="width:250px" >' + $('#metier').val() + '<span class="icon-remove"></span></a></li>');
                $.ajax({
                    type: "POST",
                    url: "Default.aspx/AjouterMetier",
                    data: "{'job': '" + $('#metier').val() + "'}",
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    success: function (msg) {
                    AjaxSucceeded(msg);
                },
                error: AjaxFailed
                });
            }
            $('#metier').val('');
        });
Sign up to request clarification or add additional context in comments.

Comments

1

Since your adding items to the session:

Session["jobs"] = jobs;

You might want to initialize the variable by grabbing it from session prior. For instance:

public void AjouterMetier(string job)
{
  if (job != "")
  {
    jobs = (List<string>)Session["jobs"] ;
    if( jobs == null )
      jobs = new List<string>();

    jobs.Add(job);
    Session["jobs"] = jobs;
  }
}

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.