Apparently it is possible to write formatted output using the <%= %>
construct (render block) in ASP.NET web forms pages and views.
<%= "{0} is {1}", "Foo", 42 %>
This will render "Foo is 42". As far as I know the ASP.NET parser translates <%= %>
into a call to HttpResponse.Write(string)
. Obviously in the code above, there is no one-to-one translation, because the number of arguments don't match (assuming the ,
in the expression above separates arguments).
Now I have seen that the class TextWriter
has a Write(string, object[])
method.
I have checked the output from the parser, and indeed it calls the TextWriter
's method that accepts a params object[]
argument for formatting:
private void @__Renderform1(System.Web.UI.HtmlTextWriter @__w, System.Web.UI.Control parameterContainer) {
// ...
@__w.Write( "{0} is {1}", "Foo", 42 );
Is that behavior documented anywhere?
The Java String. format() method returns the formatted string by a given locale, format, and argument. If the locale is not specified in the String. format() method, it uses the default locale by calling the Locale.
. NET defines a set of standard format specifiers for all numeric types, all date and time types, and all enumeration types. For example, each of these categories supports a "G" standard format specifier, which defines a general string representation of a value of that type.
The composite formatting feature returns a new result string where each format item is replaced by the string representation of the corresponding object in the list. Consider the following Format code fragment: C# Copy. string name = "Fred"; String.Format("Name = {0}, hours = {1:hh}", name, DateTime.Now);
As far as I know the ASP.NET parser translates <%= %> into a call to HttpResponse.Write(string).
Maybe the <%= "{0} is {1}", "Foo", 42 %>
is translated to Response.Output.Write(string format, params object[] arg)
, Output
being of type TextWriter
, which would be the explanation
according to http://www.hanselman.com/blog/ASPNETResponseWriteAndResponseOutputWriteKnowTheDifference.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With