0

Hello i changed the div color with javascript using css. I have a div in repeater. How can i save the changes for ever. Like if color was yellow. After even user gets offline and gets online again , the yellow color will shows in the div. how can i use this with database? Im not good at JQuery codes. Cause of that i need your help.thanks.

my code which i have to save is :

$(document).ready(function () {
              $(".divsec").on("click", function () {
                  $(this).css("background", "red");
              });
          });

my cs code is :

public static void InsertData(string Color)
{
    OleDbConnection con = new OleDbConnection(Utility.GetConnection());
    con.Open();
    OleDbCommand cmd = new OleDbCommand("INSERT INTO Temsilci(color) values(@color)", con);
    cmd.Parameters.Add("color", Color);
    cmd.ExecuteNonQuery();
    con.Close();
}

and JS code for save color to database is :

$(document).ready(function () {
    var color = '';
    var clicks = 0;
    $(".divsec").on("click", function () {
        if (clicks == 1) {
            $(this).css("background", "red");

        }
        else if (clicks == 2) {
            $(this).css("background", "green");
        }
        else if (clicks == 3) {
            $(this).css("background", "yellow");
        }
        else {
            clicks = 0;
            $(this).css("background", "transparent");
        }
        ++clicks;
        var x = $(this).css('backgroundColor');
        hexc(x);
        alert(color);

        $.ajax({
            url: 'default.aspx/InsertData',
            type:'POST',
            contentType: 'application/json;charset=utf-8',
           dataType: 'json',
           data: "{Color:'" + color + "'}",
            success: function () {
                alert("Başarıyla kaydedildi");
            },
            error: function () {
                alert("ERROR");
            }
        });


    });
    function hexc(colorval) {
        var parts = colorval.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
        delete (parts[0]);
        for (var i = 1; i <= 3; ++i) {
            parts[i] = parseInt(parts[i]).toString(16);
            if (parts[i].length == 1) parts[i] = '0' + parts[i];
        }
        color = '#' + parts.join('');
    }


});

it works fine but it send error when want to insert in database the COLOR value. And even if it perfectly will do it. I want to save the color of every div which user changed. how can i capture and save all values from repeater?

  • I think you are looking for a 'theme'. If you let the user select a CSS 'theme' and save that variable in your database, that might save you a lot of code instead of checking one or more CSS properties. If you are not looking for 'saving a CSS theme' can you please clarify a bit more what you mean? – Virginia Dec 16 '18 at 15:07
  • @Virginia for example i have to save the red color on the div in database. if user clicks and div gets red. i should insert into database color column "red" string. but with using jQuery i dont know how to do that. for that reason im ask you here. – Metin Birdal Dec 16 '18 at 15:37

1 Answers1

0

There is no way you can save the change for ever. Having said that , it is also worth mentioning that you can use localStorage , but that will also be erased once user clear browser cache and changes saved in one browser will not effect in other browser

brk
  • 48,835
  • 10
  • 56
  • 78
  • i cant use a database for this ? why i cant save forever? how the other websites does ? – Metin Birdal Dec 16 '18 at 15:15
  • @MetinBirdal you can do – brk Dec 16 '18 at 15:19
  • which codes i should use for database connection on jquery? please help me :) – Metin Birdal Dec 16 '18 at 15:27
  • You cannot connect database with jquery. You can use jquery ajax but to read write operation you need java or python like language or check this https://stackoverflow.com/questions/857670/how-to-connect-to-sql-server-database-from-javascript-in-the-browser – brk Dec 16 '18 at 15:33