SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    Luigi

  2. #2
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    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

    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.
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  3. #3
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    Luigi

  4. #4
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    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

    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:
    Code JavaScript:
     
    //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("\n");
     
        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: http://jsfiddle.net/GeekyJohn/mpNJq/

    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:

    A1 B1 C1 D1
    A2 B2 C2 D2
    A3 B3 C3 D3
    A4 B4 C4 D4
    A5 B5 C5 D5

    Analysis of a row:
    Code:
    A1  -->  B1 -->  C1  --> D1 \n
    
    ## Where --> is a TAB character
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  5. #5
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok John, I'll try.
    for now, thank you very much.

    Luigi


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •