Preserve newline characters in xml asp.net

While using a telerik text box in asp.net in multiline mode, I faced a weird issue where pressing enter was adding spaces and not new line. So, to fix that, I had to use the below css to add new line characters:

div.divTxtBox .txtBoxNewLineFix {
    width: 100% !important;
    white-space: pre !important;
}

RadTextBox aspx code:

<telerik:RadTextBox ID="txtDev"
	Wrap="true" TextMode="MultiLine" runat="server" Width="98%" CssClass="txtBoxNewLineFix" Rows="8">
</telerik:RadTextBox>

Also, while saving the text to the SQL Server database, since I was creating the XML in a C# string, the new line characters were getting replaced with new lines in the XML but it did not replicate in the saved text, so I had to preserve the \r\n characters as below:

private string preserveNewLine(string str)
{
	string sReturnValue;
	sReturnValue = str.Replace("\r\n", "
");

	return sReturnValue;
}

The below xml sample is prepared using StringBuilder in C#:

StringBuilder PlanData = new StringBuilder();
PlanData.AppendLine("<?xml version=\"1.0\" standalone=\"yes\" ?>");
PlanData.Append("<XMLInfo ");
PlanData.Append(" UserDataId=" + "\"" + UserDataId + "\"");
PlanData.Append(" UpdatedBy=" + "\"" + objUser.UserID + "\"");
PlanData.Append(" UpdatedOn=" + "\"" + DateTime.Now.ToString() + "\"");
PlanData.AppendLine(">");
PlanData.AppendLine("<Plans>");

//TO DO loop through data with following lines to add multiple Plan child elements:
Comments = preserveNewLine(Comments);
PlanData.AppendLine("<Plan ID=" + "\"" + ID + "\"" + " Comment=" + "\"" + Comments + "\"" + "/>");

PlanData.AppendLine("</Plans>");
PlanData.AppendLine("</XMLInfo>");

C# sample debug output with new lines converted:

{<?xml version="1.0" standalone="yes" ?>
<XMLInfo  UserDataId="7180" UpdatedBy="1898" UpdatedOn="10/10/2019 6:14:00 PM">
<Plans>
<Plan ID="38685" Comment="1 1111 11111 111111 1111111 11111111 1111   111111

test new line"/>
<Plan ID="38686" Comment="11  2222  2222222    222222      444"/>
}

C# sample debug output with new lines characters preserved:

{<?xml version="1.0" standalone="yes" ?>
<XMLInfo  UserDataId="7180" UpdatedBy="1898" UpdatedOn="10/10/2019 6:39:55 PM">
<Plans>
<Plan ID="38685" Comment="1 1111 11111 111111 1111111 11111111 1111   111111    

test new line"/>
}

So when this xml is saved to SQL Server and pulled back in the Text Box, the new lines will show up correctly.

Advertisements

CSS Media queries adjust to screen width in asp.net

I have two html tables in my Asp.net project, first one has normal html and 2nd one has a Repeater control. Due to some recent changes, it was difficult to align the width of the two tables. Also, I was facing issue on 2 different resolutions as below:

  1. 1920×1080 on my laptop.
  2. 1366×768 on my Desktop.

To solve this, I used the css media queries to cater to minimum width resolution of 1400px for MyTable and for above 1400px width resolution for OthersTable as shown below:

@media only screen and (max-width: 1400px) {
    .MyTable {
        width: 92%;
    }

    .OthersTable {
        width: 90%;
    }
}

@media only screen and (min-width: 1400px) {
    .MyTable {
        width: 90%;
    }

    .OthersTable {
        width: 90%;
    }
}

Media queries introduced in CSS3 let you include some properties only if certain condition is met. In the above case, I’m using screen-width as the condition. Below I’m using the class in my html tables as defined in the media queries in the aspx code.

<table id="divMyTable" visible="false" style="border-collapse: collapse;" class="MyTable" runat="server">
	<tr><td></td></tr>
</table/>			

<table id="divOthersTable" visible="false" style="border-collapse: collapse;" class="OthersTable" runat="server">
	<tr>
		<td></td>
	</tr>
</table>

Connect to a Project in TFS Visual Studio

On the Team Explorer, Click on the icon highlighted in the screen-shot below and then click on Manage Connections. Select “Connect to a Project” from the Menu.

Manage Connections is also available under the Team menu in Visual Studio.

If your Team Foundation Server is not already added then click on the “Add TFS Server” link and you’ll need to enter the TFS URL and click on Add.

You may need to enter your credentials in the pop-up window. The list below shows the Test Project, select it and click on Connect.

Finally you can map the TFS Server path to your Workspace Project i.e. Physical folder on your machine as shown below:

Useful Dev features in Visual Studio

Enable Code Lens:

Code lens is a very useful feature where the scroll button in the Visual Studio Editor is replaced by the Code Lens. It lets you take a peek into the part of code you mouse-over on the
Scroll-bar.
To enable it, go to Tools->Options and enable check-box as shown below:

You can also selectively set other code lens features as shown in the screen-shot above.

Splitter:

Above the Code Lens is the splitter as shown below. It lets you split your code-Editor screen into 2 parts when you click, hold and drag it as shown below:

The below screenshot is divided into two code-editors. On the above part, you can move around say do Ctrl+Home to go to the top of the file.

On the below part, do Ctrl+End to move to the bottom of the file.

Ctrl + M + O

This short-cut lets you collapse the entire code file as per regions.

Create #regions

Using #region and #endregion, you can divide your code-sections into collapsible regions in your .cs file.

Ctrl+;

This lets you quickly move to the search bar of your Solution Explorer and search for files in your Solution.

Navigate Forward and Backward

If you want to quickly navigate between current and previous cursor positions in same or different code files, you can do as follows:

Navigate backward: Ctrl + –
Navigate forward: Ctrl + Shift + –

The Navigation buttons are also available under the File Menu as shown:

Insert hyperlink in gmail

While composing an e-mail, it is often a requirement to add hyperlinks to some website you may want to send the person you’re writing the e-mail to. However, the url you’re trying send them may be very long, so you would to add a hyperlink to a word which in general we write as “Click here”.

So, to add the hyperlink do as follows:

  1. Click on the red highlighted icon:

2. Copy and paste the URL in the text-box as shown below and Click OK:

The link becomes clickable as shown below:

Send the e-mail.

Here is my post on how to send email with C#.net

Asp.net how to escape double quotes in text property aspx file

I recently had a facepalm moment when I experienced an issue with a text property of a user control in my aspx file that had a custom property that included double quotes. Double quotes confused the compiler and made it believe that the ID property of my user control used in cs file didn’t exist.

After wasting some time on the wrong problem, it came down to escaping the double quotes. Aspx files are like XML and need to be treated like one in the text property of attributes used in the form controls.

To escape a double quote you need to use quot; e.g.

HeaderText="This is a &quot;double quote&quot;"

will show up as below:

This is a “double quote”

Similarly for other such characters, use below ones:
 ampersand &   =>   &amp;
 single quotes '   =>   &apos;
 less than <   =>   &lt;
 greater than >   =>   &gt;