about learning and improving skills
Development
Curly braces in C# String.Format()
Jul 23rd
Ever wanted to use String.Format on, say, a javascript function? Those include curly braces ( { or } ), but those curly braces are reserved as placeholder marker in String.Format() (i.e. for {0}). Escaping the braces with a backslash (comes to mind first in C#) does not work, though.
The solution is pretty simple: You need to double those curly braces to escape them:
String.Format("function test() {{ return calcSomething({0}); }}", "123");
Pre-fill password fields in ASP.NET
Jul 23rd
At some stage I had to pre-fill a password field. Actually that is something you don’t want to do because the value can be seen in the plain html text. But perhaps you want to show some **** initially on a new user page or something.
ASP.NET won’t render the value of a TextBox with textmode set to password into the page, but you can force the required html to pre-fill password fields using the .AddAttribute(“value”, password); method on the control:
TextBox passwd = new TextBox() { TextMode = TextBoxMode.Password }; passwd.AddAttribute("value", "*****");
I already mentioned you can see the password in plain text in the html, so it is a bad idea to do that with real passwords. You should try to avoid this. But in some cases (i.e. display password strength / weakness during initial password entry on a “new user” page, with no real password but a dummy value) this totally makes sense.