0

I am building a small website with some books to sell and need to implement a simple solution to add books to a basket (maybe a Session["buylist"]) and allow the user to place an order with an eMail.

In one page i have a gridView filled with a dataReader from the SQL Database and was able to add one columne to the left with one image that calls a method in c# that takes the line/columne ISBN and adds it to the Session["buylist"].

So far so good.

In another page i have a small block with html divs and one image as buy button but the "onClick" event only works with Javascript. I tryed to call javascript with the ISBN variable and show some alert messages. I can add Session["variable"] to a "var temporary" variable inside javascript but i cannot assign the result back to the C# Session["buyList"]

And in HTML i cannot call my c# method from an image directly with "onClick event".

<script type="text/javascript">
   function fonction_ajouterPanier(ISBN) {

      window.alert('<%= Session["PanierCommande"] %>');
      var temp = '<%= Session["PanierCommande"] %>';
      temp += (" " + GenCod);

      window.alert(ISBN);

      '<%= Session["PanierCommande"] %>' =  temp;

   }
</script>

Is there an easiest way to create a list of items (integers) that stays across session pages that i can easilly reuse?

EDIT:

I tried with WebMethod but the code inside the javascript doesn't appear to be recognized:

 [WebMethod]
      protected void Fonction_ajouterPanier(string a)
      {
         Session["PanierCommande"] += a;
         Response.Write(Session["PanierCommande"]);
      }

<script type="text/javascript">
   function fonction_ajouterPanier(GenCod) {

      window.alert('<%= Session["PanierCommande"] %>');
      var temp = '<%= Session["PanierCommande"] %>';
      temp += (" " + GenCod);

      window.alert(temp);

      PageMethods.CSHARP_FunctionName(temp);

   }
</script>
mason
  • 31,774
  • 10
  • 77
  • 121
Rui Ruivo
  • 343
  • 3
  • 12
  • Maybe i can send the new value as parameter, get the parameter and simply add to the session variable in C# . – Rui Ruivo Feb 20 '19 at 13:42
  • Possible duplicate of [How to access Session variables and set them in javascript?](https://stackoverflow.com/questions/15519454/how-to-access-session-variables-and-set-them-in-javascript) – SehaxX Feb 20 '19 at 13:45
  • Reading SehaxX link. Thank you. – Rui Ruivo Feb 20 '19 at 13:47
  • Inspect that first javascript block in the browser: no "session" related stuff left. Client side code (javascript) runs at a different time than server side code (asp.net) – Hans Kesting Feb 20 '19 at 13:51

2 Answers2

0

I put this example together to give you a starting point on what you want to achieve, I hope this help you to understand better how to work with this technology.

In your WebForm, ensure that you have a ScriptManager (inside your form tag).

<asp:ScriptManager runat="server" EnablePageMethods="true"></asp:ScriptManager>

Then update your fonction_ajouterPanier function like this:

<script type="text/javascript">
    function fonction_ajouterPanier(ISBN) {
        PageMethods.Fonction_ajouterPanier(ISBN, function (result) {
            for (var item of result) {
                window.alert(item);
            }
        });
    }
</script>

Then somewhere in your html, add these two buttons for testing:

<button type="button" onclick="fonction_ajouterPanier('code1')">Add code1</button>
<button type="button" onclick="fonction_ajouterPanier('code2')">Add code2</button>

Now, in your page's code behind, update the Fonction_ajouterPanier method like this:

[WebMethod]
public static List<string> Fonction_ajouterPanier(string isbn)
{
    var session = HttpContext.Current.Session;

    var list = (List<string>)session["PanierCommande"];

    if (list == null)
    {
        list = new List<string>();
        session["PanierCommande"] = list;
    }

    list.Add(isbn);

    return list;
}

Also ensure that you have the following usings at the top of your page's code behind file:

using System.Collections.Generic;
using System.Web;
using System.Web.Services;

This will initialize the session variable PanierCommande as a List<string>, and will add some codes to it as your click on any of the provided buttons.

You can find more details about how to work with AJAX Page Methods here: https://learn.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-getting-started/aspnet-ajax/understanding-asp-net-ajax-web-services#creating-and-using-page-methods

yv989c
  • 1,453
  • 1
  • 14
  • 20
0

I was able to solve my difficulty by using the same solution that I had used before for something else. simply create a parameter in the url and detect if it is used, you can then run any code you want and simply refresh the page 1 second after with the updated results.

 string ajouterPanier = Request.QueryString["ajouterPanier"];
               if (ajouterPanier != null)
               {
                  Session["PanierCommande"] += " " + ajouterPanier;
                  Response.AddHeader("REFRESH", "1;URL=misesenvente.aspx");
               }
Rui Ruivo
  • 343
  • 3
  • 12