Data from the clipboard to my Datagrid

TheWizardofInt

Junior Contributor
Joined
Dec 31, 1969
Messages
333
Location
Orlando, FL
I want to drag an excel spreadsheet, parse the content, and move the data to a datagrid on my web page.

This needs to be done with cut and paste and, seeing as adding a mouse-event to the datagrid adds it to every member of the datagrid, making it practically unuseable, my idea was:

Put a button on the page that says, "Move data from clipboard"

Use that to kick off a clipboard reader.

It appears that the method for this lies in System.Runtime.InteropServices.ComTypes.IDataObject. Darned if I can find an example that works

Anyone been down this road before?
 
Ok, I worked this out, so I am going to share it.

The problem here, of course, is that everything you do in ASP.net is Server Side, and what you need to do here is a client side append

So you have to use javascript to capture the data client side, and then force a postback to move the data server-side, where you capture it AGAIN, and put it in your datagrid

So I created a button on the form, which was a simple HTML input button:

Code:
<input type="button", id="btnCB" value="Import From Clipboard" onclick="fnPaste()" />

Next I created the function mentioned in the onclick in the Script section of the header

Code:
function fnPaste() {
   //this captures the click event for btnCB and creates a postback
   //the is picked up in the postback of the page load, and then moved to 
   //the processarg sub
   var s = window.clipboardData.getData("Text");
  
    __doPostBack('__Page', s);
   
   //txt.focus;
}
function __doPostBack(eventTarget, eventArgument) {
   if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
       theForm.__EVENTTARGET.value = eventTarget;
       theForm.__EVENTARGUMENT.value = eventArgument;
       theForm.submit();
   }
}

And in the postback of the page, we capture the data and send it off to be processed:

Code:
            Dim eventArg As String = Request("__EVENTARGUMENT")
            If Not eventArg Is Nothing Then
                If eventArg.Length > 0 Then
                    ProcessArg(eventArg)
                End If
            End If

In an excel spreadsheet, the lines come in broken up by chr(10) + chr(13), which is the equivalent of VBCRLF. The cells are broken up by the tab, which is chr(9).

This lets you easily grab the data per line, and diseminate it per cell
 
Back
Top