Javascript function to Copy&Paste from clipboard to Textboxes

Hello all,
I have to write a javascript function that let me Copy&Paste values from the Clipboard to
several textboxex that I have in ASP.NET Datagrid.

In ITemDataBound event I have this code:

private void grdData_ItemDataBound(object sender, DataGridItemEventArgs e)

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
TextBox txtProposta1 = (TextBox)e.Item.FindControl(“txtProposta1”);
TextBox txtProposta2 = (TextBox)e.Item.FindControl(“txtProposta2”);

TextBox txtProposta3 = (TextBox)e.Item.FindControl(“txtProposta3”);
TextBox txtProposta4 = (TextBox)e.Item.FindControl(“txtProposta4”);
TextBox txtProposta5 = (TextBox)e.Item.FindControl(“txtProposta5”);
TextBox txtProposta6 = (TextBox)e.Item.FindControl(“txtProposta6”);
TextBox txtProposta7 = (TextBox)e.Item.FindControl(“txtProposta7”);
// …etc …

// Eventi
txtProposta1.Attributes.Add(“onpaste”, “return onPaste(this)”);
txtProposta2.Attributes.Add(“onpaste”, “return onPaste(this)”);
// …etc …


so I have to write to this function - onpaste - that copy the values in these textboxes.

This task is row by row, not in group (so it could be more easy to write).

Anyone can help me in this task?

Thanks a lot.


Hey Luigi,

What’s the scenario here? (Explain your use case for where the data comes from and where it needs to go.)

It seems to me that if you have data in a .NET DataGrid (that’s in the same web application), there might be an easier way to copy that data across. Probably a way that doesn’t use the clipboard. Unless your data is coming from an external website, and even then it might be easier to load this data in differently :slight_smile:

With your current setup, you would have to analyse the clipboard data and split it based on some parameter to then put it in to the individual textboxes. Additionally, you might not have access to the clipboard data, depending on the browser your using.

The data comes from a row in Excel file, and I have to paste it in a row of my Datagrid,
starting from the Textbox where the user click.

I’m using only IE, and my scope is just to paste them in these textboxes.


Right, well that shouldn’t be too hard. I don’t have MS Excel here, but I have OpenOffice Calc, so I tested with that.

In a few browsers, when you watch for the Paste event, they will attach a clipboardData object to the event, in Internet Explorer however it is not present, but they have kindly provided it on the Window object, so it is kind of always there.

If you’re getting data from a Paste event, most browsers shouldn’t throw a warning either, another plus :slight_smile:

I wrote a solution for this that is slightly different than your requirement, I wrote it so that a user can paste in to a special “paster” field that I watch for paste events, when the user pastes in to that field, I take the paste data, split it by newline to get the rows, and then populate the form fields with that data. You can quite easily adapt this to make it work from the currently pasted field forward I imagine.

The basics are:

//assume we have a form field we're pasting to:
document.getElementById("paster").addEventListener("paste", pasteHandler);

function pasteHandler(e){
    var pasted, rows, fields, fieldsLen, i;

    // use either 
    // pasted = e.clipboardData.getData("text")
    // or 
    // pasted = window.clipboardData.getData("text")

    // split by newline and populate fields
    rows = pasted.split("\

    fields = document.getElementsByClassName("inputRow");
    fieldsLen = fields.length;

    //loop through your data fields and assign the values.
    for (i = 0; i < fieldsLen; i+=1) {
        //assign value of paste data to the field
        fields[i].value = rows[i] || "";

I have a full solution on JS Fiddle if you want to take a look:

As I mentioned at the start, I don’t have MS Excel, so you might need to adapt the string splitting part depending on what it does, but I’m assuming it does either the same or something very similar to OpenOffice.
e.g. for my test data I used a simple dataset:


Analysis of a row:

A1  -->  B1 -->  C1  --> D1 \

## Where --> is a TAB character

Ok John, I’ll try.
for now, thank you very much.