Im currently using ASP.NET 4.7 webforms for a project, I'm trying to copy the data that is generated in the backend to users client machine.
In other words, when the user submits the form, the code that is generated in the backend needs to be copied to the users clipboard.
Here is my attempt.
Application.aspx.cs
.
.
.
protected void Submit(object sender, EventArgs e)
{
try
{
ClientScript.RegisterStartupScript(GetType(), "hwa", "copyToClipboard("+encodedValue+");", true);
}
catch (Exception ex)
{
}
}
Application.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Application.aspx.cs" Inherits="azureCopyToClipboard.Application" %>
<!DOCTYPE html>
<form id="form1" runat="server">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<div>
<asp:Button ID="SubmitButton" runat="server" class="btn btn-primary " OnClick="Submit" Text="Submit" />
</div>
</body>
</html>
<script type="text/javascript">
function copyToClipboard(textToCopy) {
var textArea;
function isOS() {
//can use a better detection logic here
return navigator.userAgent.match(/ipad|iphone/i);
}
function createTextArea(text) {
textArea = document.createElement('textArea');
textArea.readOnly = true;
textArea.contentEditable = true;
textArea.value = text;
document.body.appendChild(textArea);
}
function selectText() {
var range, selection;
if (isOS()) {
range = document.createRange();
range.selectNodeContents(textArea);
selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
textArea.setSelectionRange(0, 999999);
} else {
textArea.select();
}
}
function copyTo() {
document.execCommand('copy');
document.body.removeChild(textArea);
}
createTextArea(textToCopy);
selectText();
copyTo();
}
</script>
</form>
im calling the client function using ClientScript.RegisterStartupScript to paste the encoded value to the users clipboard...however this attempt is not working due to modern browser security reasons, I'm also not getting any client side errors to attach to this question.
Im trying to make this solution support both mobile & non-mobile.