From classical ASP days if we want to print some string with formatting we used to make use of Response.Write and some function to do the string formatting for us. But in .NET we have Response.Output.Write which is equal to Response.Write + String.Format features.
Find below the basic sample explaining this feature!
private void Page_Load (object sender, System.EventArgs e)
{
DateTime dtTwoDays = DateTime.Now; // Get the current date
dtTwoDays = dtTwoDays.AddDays(2); // Add 2 days to it
Response.Write(”Classical way of doing the same thing”);
string strMessage = dispMessage(dtTwoDays); // Call a method which would return the formatted string.
Response.Write(strMessage); // Print that formatted string
Response.Write(”.NET way of doing the same thing:”);
Response.Output.Write(”{0} from today is {1:d}”, “Two days”, dtTwoDays);
}
Private string dispMessage(DateTime dtMVP)
{
// {0} and {1:d} are the place holders which would take up the values passed as parameters
// {0} will take “Two days“ and
// {1:d} will take the value of dtMVP
return String.Format(”{0} from today is {1:d}”, “Two days”, dtMVP);
}
While using this code don't forget to include “BR” tags where ever necessary. So that the output would be well formatted. If not, everything would be printed in a single line. Hope this helps!
Find below the basic sample explaining this feature!
private void Page_Load (object sender, System.EventArgs e)
{
DateTime dtTwoDays = DateTime.Now; // Get the current date
dtTwoDays = dtTwoDays.AddDays(2); // Add 2 days to it
Response.Write(”Classical way of doing the same thing”);
string strMessage = dispMessage(dtTwoDays); // Call a method which would return the formatted string.
Response.Write(strMessage); // Print that formatted string
Response.Write(”.NET way of doing the same thing:”);
Response.Output.Write(”{0} from today is {1:d}”, “Two days”, dtTwoDays);
}
Private string dispMessage(DateTime dtMVP)
{
// {0} and {1:d} are the place holders which would take up the values passed as parameters
// {0} will take “Two days“ and
// {1:d} will take the value of dtMVP
return String.Format(”{0} from today is {1:d}”, “Two days”, dtMVP);
}
While using this code don't forget to include “BR” tags where ever necessary. So that the output would be well formatted. If not, everything would be printed in a single line. Hope this helps!
Comments