1

I am interested in saving text when user selects any text anywhere on a web page that text has to be highlighted and has to save that text as a string in C#.

when same user saw same page next time text has to be highlighted as done previously by him. If anyone knows a simple elegant way to do this I would really appreciate it, but I'll take any solution at this point. Even if you can point me in the right directions that would be appreciated.

Thanks in advance.

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • It has to be on the client side using javascript – Sandeep G B Sep 21 '11 at 09:14
  • this is an interesting question, I think you should use some kind of Javascript/Jquery call to get what is selected, not sure this is possible at all actually, then you would need to pass this information to the server and save it then retrieve it at next load of the page by same user. For you Anywhere on the page means every label or control or just one or two text boxes you have in your page? – Davide Piras Sep 21 '11 at 09:15
  • Possible duplicate of [1]: http://stackoverflow.com/questions/2557703/manipulate-highlighted-text-with-javascript [2]: http://stackoverflow.com/questions/5379120/jquery-get-the-highlighted-text – Sandeep G B Sep 21 '11 at 09:16

3 Answers3

0

You need to write service WCF or Webservice in server side that will receive userId and text and save it into database.

[WebMethod(Description = "Save Text")]
        public string Savetext(int userId ,string text)
        {
}

Second method will retrive the text from daatabase by user id

 [[WebMethod(Description = "Get text")]
        public string GetText(int userId)
        {}

In client side do invokation using Ajax calls (Jquery)

Gregory Nozik
  • 3,296
  • 3
  • 32
  • 47
0

you can do that by capturing the selected text and send it via ajax call to your database. check this example to know haw you can capture the selected text.

If you use jquerythen you will use select() function to capture the selected text

<textarea id="txt"></textarea>
<script>
    $(document).ready(function(){
      $('textarea').select(function()
      {
        var selectedText=window.getSelection();
        //here put the ajax call to your webservice
      });
   });
</script> 
Amir Ismail
  • 3,865
  • 3
  • 20
  • 33
0

Use this to get selected text from page: http://mark.koli.ch/2009/09/use-javascript-and-jquery-to-get-user-selected-text.html

Then in mouseup event copy it to some HiddenField. Now you need a button or maybe AJAX call in mouseup so you can send it to server. On the server side save it to DB along with UserID and page address or ID.

On the next visit of this user to this page check in DB for entry. If it exist put the text in some hidden field. Then using jQuery get that text clientside, find it on page (using regex or something) and select it. You should remember to disregard any HTML markup while finding the text which can be tricky...

That is general way I would take to do this.

Episodex
  • 4,479
  • 3
  • 41
  • 58