Monday, September 14, 2009

ASP.Net Setting Text for a Textbox with TextMode = Password

When one sets the Text property in a TextBox with TextMode = Password, the default behaviour is to render the TextBox empty.
Basically for security reasons and that's the normal and correct behaviour.
But and there is always a BUTT, one may want to set the Text in a TextBox with TextMode = Password.
The way to do it is as follows:

txtPwd.Attributes.Add("value", customer.Password);

Tuesday, September 8, 2009

Call JQuery code from ASP.Net server side

A little trick to call JQuery code from the server side.
A typical example is that when one wants to show different panels of a wizard or when a form is submitted successfully, one wants to show 'Congratulations' page.

Basically one just needs to inject the Javascript code in the server side code and JQuery will execute it once it is in control.
Like the following code snippet:

StringBuilder
sb = new StringBuilder();
sb.AppendLine("$(document).ready(function() {");

sb.AppendLine("showForm();");
sb.AppendLine(" });");

Page.ClientScript.RegisterClientScriptBlock(typeof(Page),Guid.NewGuid().ToString(), sb.ToString(), true);

Sidenote, the JQuery document ready function can be in an external javascript file. The above will still work.