Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Nuts N Bolts of ASP.net

0.00/5 (No votes)
19 Jan 2011CPOL 8.2K  
Append New line in multiline TextBox
I had to develop a short application as an enhancement in the existing project. The application had to collect data from database and display it in gridview with CheckBoxes. The checked row data had to be sent on clipboard in row wise. To do this, I stored the strings in an array and I had to assign the string array in multiline Textbox appended by newline character. Surprisingly:

C#
for (int i = 0; i < gvCheckboxes.Rows.Count; i++)
            {
                bool chkBoxChecked = ((CheckBox)gvCheckboxes.Rows[i].FindControl("chkBxSelect")).Checked;
                if (chkBoxChecked == true)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        _collectCellValue[j] = gvCheckboxes.Rows[i].Cells[j+1].Text.ToString();
                    }
                     _result = ConvertStringArrayToString(_collectCellValue);
                     _stringOnClipBoard[i] = _result;
        :omg: //  TextBox1.Text= _stringOnClipBoard[i]+"\r\n"   Failed to append Newline character.      
                }
             }


So I had to do as below and it did work for append newline character after each string array element :-O :

MIDL
foreach (string string4Clipoard in _stringOnClipBoard)
           {
               if (string4Clipoard != null)
               {
                   TextBox1.Text = TextBox1.Text + string4Clipoard + "\r\n";
               }
           }


Another issue I struggled is with Clipboard copy as the following code didn't work, God knows why, though it did work for my test work but failed on the project:

MIDL
string CopyToClipboard;
CopyToClipboard = "<script type='text/javascript' language='javascript'>function CopyToClipboard(){";
CopyToClipboard += "window.clipboardData.setData('TEXT', '" + TextBox1.Text + "');}CopyToClipboard();</script>";
Response.Write(CopyToClipboard);


So I had to use PageRegister approach as given below:

VB
Page.RegisterStartupScript("MyScript",
                                      "<script language=javascript>" +
                                "   var Data;  Data = document.getElementById('" + TextBox1.ClientID + "').value; window.clipboardData.setData('TEXT', Data); </script>");


These are easy looking, but at times it becomes very tough to solve. Hope it becomes a time saver for someone. God help all developers. :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)