65.9K
CodeProject is changing. Read more.
Home

Nuts N Bolts of ASP.net

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Jan 19, 2011

CPOL
viewsIcon

8308

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:
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 :
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:
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:
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. :)