0

I have a button "copy" which should copy a before generated text to clipboard.

I have the following code in my SharePoint-WebPart-Application:

public void CopyToClipboard_Click(object sender, EventArgs e)
    {
        string text = TextBox1.Text;
        ScriptManager.RegisterStartupScript(CopyButton, CopyButton.GetType(), "Copy", "clipboardData.setData('text', '" + text + "');", true);
    }

The weird thing is that, when I insert my own text in the TextBox1 and press the CopyButton, the text will be copied, but if I want to copy the generated text to the clipboard, nothing happen.


ok, The problem is not really solved. The functionality copy to clipboard is done, but through removing the escape sequences, the text is not formatted: no new lines, the text is in a row.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
grekko
  • 53
  • 1
  • 11

2 Answers2

0

I would forget about the postback and add an event handler on the textbox for when it loses focus and then use javascript to copy the contents to the clipboard (and remember, some users can prevent this from happening).

See this SO question for answers on the code to copy to the clipboard via javascript.

Community
  • 1
  • 1
Josh
  • 10,352
  • 12
  • 58
  • 109
0

I fixed the problem by removing the escape sequences in the generated text with text.Replace("/n", "").

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
grekko
  • 53
  • 1
  • 11