Easy method for doing a Copy and Paste that works in all browsers using ASP.Net

The Task

I recently had a task for users to easily copy information displayed in a Gridview, then paste it into another application. They only needed a few pieces of information from the GridView to be copied. I searched Google for some quick code that I could plug in and make it possible. I quickly found some solutions, but, they only worked for Internet Explorer. That’s a problem because my customer only uses Google Chrome. I tested what I thought were the top 5 of the solutions, but I could not get them to work with Google Chrome, I did eventually solve the problem, but, it was easy 10+ hours later with trial and error. I thought my solution might help others, so here goes.

The HTML Page

In my ASPX page I added a LinkButton and a Hidden field inside my GridView’s TemplateField where I also had my existing checkbox to make a row selection. I placed the LinkButton just below it so it would be easy for the user’s to click right away and not have to scroll right, just click, go to their other application and paste. Just a note about security, there is a security issue because you really don’t want a website you visit to have control of your computers clipboard (where the information is temporarily stored during a copy and paste). For that reason a user interaction has to be performed (in this case a LinkButton), which signals that the operation is OK to do.


                        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" AutoGenerateColumns="False" DataKeyNames="ReportID" HorizontalAlign="Left" ShowFooter="True" AllowSorting="True" OnSorting="GridView1_Sorting">
                            <Columns>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:CheckBox ID="CB1" runat="server" onclick="Check_Click(this)" ClientIDMode="Static" />
                                        <asp:LinkButton ID="lbCopyClipboard" ClientIDMode="Static" runat="server"><span style="font-size:12px;background-color:paleturquoise;">CB</span></asp:LinkButton>
                                        <asp:HiddenField ID="CBData" ClientIDMode="Static" runat="server" Value='<%# GatherCB(Eval("Project"), Eval("Job_No"), Eval("Service1"), Eval("Service2"), Eval("Service3"), Eval("Service4"), Eval("Time"), Eval("Technician"))%>'/>
                                    </ItemTemplate>
                                    <ItemStyle Width="20px" />
                                </asp:TemplateField>
... (rest of the gridview specification)...

Looking at the code you can see that the actual clipboard data I’m going to send is in the HiddenField I created a “GatherCB” function to format the data I’m going to send to the clipboard.

Add this Javascript to your documents Head section

     <script>
        function copyToClipboard(thistext) {

            // Create a "hidden" input
            var aux = document.createElement("input");

            // Assign it the value of the specified element
            aux.setAttribute("value", thistext);

            // Append it to the body
            document.body.appendChild(aux);

            // Highlight its content
            aux.select();

            // Copy the highlighted text
            document.execCommand("copy");

            // Remove it from the body
            document.body.removeChild(aux);

        }

    </script>

Now the Codebehind…

First I’ll start with my “GatherCB” function, so you can see how I assembled the data that I’m going to copy to the clipboard, then the actual function that copies the text from the LinkButton, this code is vb, but you can easily convert it to c# with an online converter there is one available from telerix here.



    Protected Function GatherCB(PJName As String, PJNo As String, Svc1 As String, Svc2 As String, Svc3 As String, Svc4 As String, Tme As String, Tch As String) As String
        Dim Ret As String = ""
        Dim MyServices As String = ""
        If Svc1.Trim <> "" Then
            MyServices = " " & Svc1.Trim
        End If
        If Svc2.Trim <> "" Then
            MyServices &= ", " & Svc2.Trim
        End If
        If Svc3.Trim <> "" Then
            MyServices &= ", " & Svc3.Trim
        End If
        If Svc4.Trim <> "" Then
            MyServices &= ", " & Svc4.Trim
        End If
        If Tch.Trim = "" Then
            Ret = PJName.Trim & " " & PJNo.Trim & MyServices & " / " & Tme.Trim & " No tech has been assigned yet "
        Else
            Ret = PJName.Trim & " " & PJNo.Trim & MyServices & " / " & Tme.Trim & " " & Tch.Trim
        End If
        Return Ret
    End Function

in the GridView Row databound event...

    Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            ' Copy to Clipboard
            Dim button As LinkButton = DirectCast(e.Row.FindControl("lbCopyClipboard"), LinkButton)
            Dim CBData As HiddenField = TryCast(e.Row.FindControl("CBData"), HiddenField)
            button.Attributes.Add("onclick", "copyToClipboard('" & CBData.Value & "');")
        End If
    End Sub

That’s it, a cool and simple way, it works with most browsers. Need something unique for your business computing? Give me a call 972.571.4808.

Manually Reset or Clear the Dirty Bit in Windows without CHKDSK or a Hex Editor

Another hidden gem

Recently I had a multi-boot system when booting Windows Server 2008 it would constantly want to do a consistency check when starting. If I let it run the consistency check it would corrupt the boot volume. So, anytime I rebooted I had to remember to quickly press any key to stop the automatic disk check process. Here’s a screenshot of what I’m talking about.

I thought there has to be a way to reset that dirty bit. So, I started searching the internet. This ended up being a much bigger deal than I had imagined. Just about every site suggested using a hex editor to manually set the bit on the byte that contains the dirty bit location. Well, that’s just great if you don’t mind taking the risk. I attempted to use the utilities but I could never find a match for the byte that was listed. I didn’t want to just try it on a close match and make the drive completely not bootable. So, I kept searching…

Finally I found CHKNTFS.EXE on a Microsoft website the original article can be found here. I was pretty surprised all the other solutions could be so potentially dangerous, and here was a simple utility to do exactly what I wanted. Here’s how to use it and for this tutorial we’ll assume the drive I want to stop checking is drive C.

How to use CHKNTFS.EXE to reset the dirty bit

First you’ll want to open an elevated command prompt. Just go to your start menu, find “Windows System”, then right click “Command Prompt” and select “More”, then “Run as Administrator”.

From here you’ll get a command prompt…

 

Now, type chkntfs /x c:

Note that it doesn’t return anything, so you might think it didn’t work. But, if you don’t get an error about incorrect syntax you’re good. Also, there are other switches that might be useful to see those check the link above. If you need computer hardware, networking support, any type of custom application work and are in the Dallas / Ft. Worth area I’d be happy to help, just give me a call 972.571.4808.

I can map the drive by IP but not name

The Other Options Didn’t Solve the Problem

When I had the problem of being able to see a shared drive on the file server only by using the IP address and not the name I began searching the internet for answers, some workstations would see it by name and some wouldn’t. There is a mixture of XP, Windows 7, and Windows 10 machines on the network and some of each different type would connect while others wouldn’t. When I searched the Internet I found solutions about going to ‘Credential Manager’ (Control Panel, Credential Manager, Windows Credentials) and removing the saved credentials and that seemed to solve the problem for a lot of people, but not for me.

Back Story / How I got in this Mess

The machine that the file server was on crashed. The drive was fine, but the system itself stopped working. After tracing it to the motherboard I knew there wasn’t going to be a quick fix. This is a small company and they only have one other machine that is used as the SQL server, there old machines, but were pretty good when they were purchased years ago. Anyway, I needed to get this drive up as soon as possible, so not having time to get a new machine and do a bare metal restore, I took down the SQL machine long enough to install the file server drive into it. Then installed Virtualbox, and having the virtual computer use the physical hard drive of the server computer. Whala (voila if your french), file server is back online inside the SQL server box. (They both had similar hardware that aided in making this possible).

As we move ahead in time a few weeks, I built a new machine and kept the old server drive as a virtual drive except converted the physical drive to an actual virtual drive. My thought here was that later I can virtualize both of them (file server and SQL server) and come up with a clustering solution spread over a couple physical machines. So, after putting it all together on a new machine and firing it up suddenly not all workstations see the shared folder on the file server. This is where the trauma begins…

The Solution

I used Windows 10 for the pictorial solution, but, if your familiar with things, just un-check IP version 6 from the list of protocols from the workstations that are having the problem and things should work for you too.

1. First, right click on you network taskbar icon

2. Then when the settings window opens select “Ethernet”

3. Next Select “Change Adapter Options”

4. On the next window “Right Click” on the “network adapter” and select “properties”

5. Next, Un-check the IP Version 6 protocol and select “OK”

Conclusion

You can’t make this stuff up. Anytime when you least expect it something will happen. I handled this with very little user disruption. Even when the server crashed, the time back up was under 3 hours and when the IPv6 problem happened it was intermittent and I could handle most of that over the weekend. Need expert help with your network? In the Dallas / Ft. Worth Area, call me. 972.571.4808.