Click here to Skip to main content
15,867,141 members
Articles / Programming Languages / C#
Tip/Trick

Nuts N Bolts of ASP.net

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
19 Jan 2011CPOL 8K   2  
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)


Written By
Software Developer
India India
The Author holds degrees in Bachelor as well as Master of Computer Application. He firmly believes in the philosophy 'Sound Mind in Sound Body'. He loves Jogging, exersising and you may find him lost in deep thoughts when free.He feels Necessity is the mother of all logics and Programming can't be work but challenge whose output is fun n happiness.Bathroom is the place where he finds heaven. For a cloud application demo please do visit
https://www.youtube.com/watch?v=klGX6U0uURI

Comments and Discussions

 
-- There are no messages in this forum --