Thursday, December 16, 2010

Clipboard(Copy and Paste) in ASP.NET

javascript clipboardData Description

 

The clipboardData object (accessible as a property of a window or frame object) is a temporary container that scripts in IE 5 and later for Windows can use to transfer text data, particularly during script-controlled operations that simulate cutting, copying, and pasting, or that control dragging. Your script controls what data is stored in the clipboardData object, such as just the text of an element, an element's entire HTML, or the URL of an image. For example, a page for children could display simple icon images of several different kinds of animals. If the user starts dragging the dog icon, the script initiated by the img element's onDragStart event handler stores a custom attribute value of that element (perhaps the URL of a pretty dog photo) into the clipboardData object. When the user drops the icon into the designated area, the onDrop event handler's function reads the clipboardData object's data and loads the photo image into position on the page.
Data stored in this object survives navigation to other pages within the same domain and protocol. Therefore, you can use it to pass text data (including arrays that have been converted to strings by the Array.join( ) method) from one page to another without using cookies or location.search strings. But this is not the system clipboard (for security reasons).
For more information on transferring data via this object and the event.dataTransfer object, visit http://msdn.microsoft.com/workshop/author/datatransfer/overview.asp.
HTML Equivalent
None.
Object Model Reference
[window.]clipboardData
Object-Specific Properties
dropEffecteffectAllowed
Object-Specific Methods
clearData( )getData( )setData( )
Object-Specific Event Handler Properties
None.

 


javascript clipboardData setData( )

 

setData(dataFormat, stringData)
  
Stores string data in the clipboardData object. Returns Boolean true if the assignment is successful
Parameters

dataFormat
A string specifying the format for the data to be read. Earlier plans to allow multiple data types appear to have fallen through. As of IE 6, the only reliable format is Text. While the method accepts URL as a format, reading a set value in that format is not successful.
stringData
Any string value, including strings that contain HTML tags.
Returned Value
Boolean value: true | false.

javascript clipboardData getData( )

getData(dataFormat)
  
Returns a copy of data from the clipboardData object. The clipboardData contents remain intact for subsequent reading in other script statements.
Parameters

dataFormat
A string specifying the format for the data to be read. Earlier plans to allow multiple data types appear to have fallen through. As of IE 6, the only reliable format is Text.
Returned Value
String.

javascript clipboardData clearData( )

clearData([dataFormat])
  
Removes data from the clipboardData object.
Parameters

dataFormat
An optional string specifying a single format for the data to be removed. Earlier plans to allow multiple data types appear to have fallen through. As of IE 6, the only reliable format is Text. Omitting the parameter removes all data of all types.
Returned Value
None.

javascript clipboardData dropEffect, effectAllowed

These two properties belong to the clipboardData object by inheritance from the dataTransfer object, to which they genuinely apply. Ignore these properties for the clipboardData object.

 Example :

function SaveClipboardData(con) //Copy Clipboard Data from control... con is the control
        {
           var data = con.innerText ? con.innerText : con.textContent;
           window.clipboardData.setData("Text", data);
           alert(data);
        }
        function CopyToTextBox() //Inserting ClipBoard Data into TextBox
        {
            var data = window.clipboardData.getData("Text");
            alert("Clipboard Paste = \n\n" + data);
            var txt = document.getElementById('<%= txtEmailBody.ClientID %>');//TextBox
            var mosPos = 0;
            if (document.selection)
            {
                txt.focus();
                var range = document.selection.createRange();
                range.text = data;
            }
            else if (txt.selectionStart != null)
            {
                mosPos = txt.selectionStart;
                var strFirst = txt.value.substring(0, mosPos);
                var strLast = txt.value.substring(mosPos);
                if (txt.value == "")
                {
                    txt.value = data;
                    txt.focus();
                    con.className = "variable_selected";
                }
                else
                {
                    txt.value = strFirst + data + strLast;
                    txt.focus();
                    con.className = "variable_selected";
                }
            }
        }


 

 

 


No comments:

Post a Comment