Original Thought
public class Vehicle
Car y = Factory.GetBMW();
x.Drive();
y.Drive();
Developing for Microsoft platform
This article demonstrates how to generate perfect HTML using the RenderControl method of server controls. The Html code is generated without makeing any server-side state or view state.
/// <summary>
/// This method demonstrates how to show Html output of a Server Control
/// </summary>
private void ShowHtml()
{
// Create the in-memory objects that will catch the rendered output.
StringWriter writer = new StringWriter();
HtmlTextWriter output = new HtmlTextWriter(writer);
// Render the control to an in-memory string.
var control = new HyperLink();
control.NavigateUrl = @”http:\asghar.panahy.com”;
control.Text = “Asghar Panahy”;
control.Target = “_blank”;
control.ToolTip = “Demostration”;
control.RenderControl(output);
// Display the HTML (and encode it properly so that
// it appears as text in the browser).
lblHtml.Text = “The HTML for LinkWebControl1 is<br /><blockquote>”
+ Server.HtmlEncode(writer.ToString()) + “</blockquote>”;
}
The output of this codewill be reading what you would ecpect in the Html code behind.
Notice that skipping the Server.HtmlEncode call will put the generated html as a link into the page.
descriptor.AddProperty(
“weakCssClass”, this.WeakCssClass);reference.Assembly =
“Panahy.Ajax”;if (sMgr == null)
This article explainis the basic steps for impplementing your own javascript class and use it in a page.
First, you need to write a script that describes the class. This example simply provides a class that verifies the length of a word to see if it has enough length to be used as a password. The file name is PasswordStrengthComponent.js
/* When working with JavaScript files in the code editor, you can add a reference to the Microsoft AJAX Library. This will ensure that your coding includes IntelliSense for the library. This is similar to the using statement in C# and the Imports statement in Visual Basic. You embed this reference in a comment at the top of your .js file. The following shows an example.
*/
/// <reference name=”MicrosoftAjax.js”></reference>
// register your namespace
Type.registerNamespace(“Panahy.Ajax”);
//create constructor
Panahy.Ajax.PasswordStrengthComponent = function () {
Panahy.Ajax.PasswordStrengthComponent.initializeBase(this);
}
//define class
Panahy.Ajax.PasswordStrengthComponent.prototype = {
initialize: function () {
//add custom initialization here
Panahy.Ajax.PasswordStrengthComponent.callBaseMethod(this, ‘initialize’);
},
passwordStrengthClass: function (password) {
var strPass = new String(password.toString());
if (strPass.length < 5) {
return “Weak”;
}
else if (strPass.length < 8) {
return “Medium”;
} else {
return “Strong”;
}
},
dispose: function () {
//add custom dispose actions here
Panahy.Ajax.PasswordStrengthComponent.callBaseMethod(this, ‘dispose’);
}
}
//register class as a Sys.Component
Panahy.Ajax.PasswordStrengthComponent.registerClass(
‘Panahy.Ajax.PasswordStrengthComponent’, Sys.Component);
//notify script loaded
if (typeof (Sys) !== ‘undefined’) Sys.Application.notifyScriptLoaded();
Next, you reference this script file in your page within the ScriptManager:
<asp:ScriptManager ID=”ScriptManager1″ runat=”server”>
<Scripts>
<asp:ScriptReference Path=”Scripts/PasswordStrengthComponent.js” />
</Scripts>
</asp:ScriptManager>
From this point the script is available and can be used in thepage as in the following example:
<script language=”javascript” type=”text/javascript”>
function _OnKeypress() {
var checker = new Panahy.Ajax.PasswordStrengthComponent();
var pass = document.getElementById(“MainContent_TextBoxPassword”).value;
var strength = checker.passwordStrengthClass(pass);
document.getElementById(
“MainContent_TextBoxPassword”).setAttribute(“class”, strength);
}
</script>
<asp:TextBox ID=”TextBoxPassword” runat=”server”
TextMode=”Password” Width=”200″ onkeyup=”_OnKeypress()” CssClass=”Empty”></asp:TextBox>
Next part will take this subject one step forward.
You can pass the results of a linq query to the next statement like this one
You can load a server control to your page using the constructor of the control. For example a Literal control can be loaded as follows:
plhContainer.Controls.Add(
new Literal
{
Text = string.Format("{0}.{1}",
method.ReflectedType.FullName, method.Name)
});
When it comes to a custom control you also want the markup code of you custom control to be loaded too. So you need to tell the Page o load it for you:
var nameBox = (NameBox) Page.LoadControl("NameBox.ascx");
Basically, you could do this at any time, but it is recommanded to do this at Page_Load the reason is that this is the best place for the control to restore its state and receive postback events. Also the binding will take place after this method. Look at my previous post for the sequence of the events and method calls when page gets loaded.
It is also recomannded to set a unique ID to that control if you need to find that later using FindControl, or some one else want to find where you have put it 🙂
So, my load method will look like this:
private void LoadMyControls()
{
var nameBox = (NameBox) Page.LoadControl("NameBox.ascx");
// Give the user control a unique name by setting its ID property.
// You can use this information to retrieve a reference to the control
// when you need it with the Page.FindControl() method.
nameBox.ID = "nameBox";
nameBox.FirstName = "Asghar";
nameBox.LastName = "Panahy";
nameBox.ChangeRequest += ChangeName;
plhContainer.Controls.Add(nameBox);
}
When I call LoadMyControls() in Page_Load method, I see the following sequence in my output:
STARTING: MCTS._70_515.Resources._Default.Page_Init
STARTING: MCTS._70_515.Resources._Default.OnInit
STARTING: MCTS._70_515.Resources._Default.Page_Load
STARTING: MCTS._70_515.Resources.NameBox.Page_Init
STARTING: MCTS._70_515.Resources.NameBox.OnInit
STARTING: MCTS._70_515.Resources._Default.OnLoad
STARTING: MCTS._70_515.Resources.NameBox.Page_Load
STARTING: MCTS._70_515.Resources.NameBox.OnDataBinding
STARTING: MCTS._70_515.Resources.NameBox.get_LastName
STARTING: MCTS._70_515.Resources.NameBox.OnLoad
STARTING: MCTS._70_515.Resources._Default.OnPreRender
STARTING: MCTS._70_515.Resources.NameBox.OnPreRender
STARTING: MCTS._70_515.Resources.NameBox.OnUnload
STARTING: MCTS._70_515.Resources._Default.OnUnload
Notice that the custom control gets its data binded just after the page has passed Loading which is a good thing.
When I put a logging in a couple of override methods of a NameBox control and load the page containing the custom control I see the following:
STARTING: MCTS._70_515.Resources.NameBox.Page_Init
STARTING: MCTS._70_515.Resources.NameBox.OnInit
STARTING: MCTS._70_515.Resources.NameBox.Page_Load
STARTING: MCTS._70_515.Resources.NameBox.OnDataBinding
STARTING: MCTS._70_515.Resources.NameBox.get_LastName
STARTING: MCTS._70_515.Resources.NameBox.OnLoad
STARTING: MCTS._70_515.Resources.NameBox.OnPreRender
STARTING: MCTS._70_515.Resources.NameBox.OnUnload
It is important to note that Page_Load is long before OnLoad method, similar to Page_Init versus OnInit.
Next I click on the change button to post back to handle the event and I see the following:
STARTING: MCTS._70_515.Resources.NameBox.Page_Init
STARTING: MCTS._70_515.Resources.NameBox.OnInit
STARTING: MCTS._70_515.Resources.NameBox.Page_Load
STARTING: MCTS._70_515.Resources.NameBox.OnDataBinding
STARTING: MCTS._70_515.Resources.NameBox.get_LastName
STARTING: MCTS._70_515.Resources.NameBox.OnLoad
STARTING: MCTS._70_515.Resources.NameBox.ChangeRequestClicked
STARTING: MCTS._70_515.Resources.NameBox.OnPreRender
STARTING: MCTS._70_515.Resources.NameBox.OnUnload
Next, I add some similar loggging to the page that contains the control and see what happens when I load the page:
STARTING: MCTS._70_515.Resources.NameBox.Page_Init
STARTING: MCTS._70_515.Resources.NameBox.OnInit
STARTING: MCTS._70_515.Resources._Default.Page_Init
STARTING: MCTS._70_515.Resources._Default.OnInit
STARTING: MCTS._70_515.Resources._Default.Page_Load
STARTING: MCTS._70_515.Resources._Default.OnLoad
STARTING: MCTS._70_515.Resources.NameBox.Page_Load
STARTING: MCTS._70_515.Resources.NameBox.OnDataBinding
STARTING: MCTS._70_515.Resources.NameBox.get_LastName
STARTING: MCTS._70_515.Resources.NameBox.OnLoad
STARTING: MCTS._70_515.Resources._Default.OnPreRender
STARTING: MCTS._70_515.Resources.NameBox.OnPreRender
STARTING: MCTS._70_515.Resources.NameBox.OnUnload
STARTING: MCTS._70_515.Resources._Default.OnUnload
So, The OnLoad method gets called after binding whereas the Page_Load is about before the binding. Obviously, if you need to initialize your data, you better put your code in OnInit rather that OnLoad or Page_Load.
Another important thig here is the PreRender methods. These methods get called after load has been completed and the data is assumed to be in place.
Now let’s see what happens when I click a button on the control which causes a postback:
STARTING: MCTS._70_515.Resources.NameBox.Page_Init
STARTING: MCTS._70_515.Resources.NameBox.OnInit
STARTING: MCTS._70_515.Resources._Default.Page_Init
STARTING: MCTS._70_515.Resources._Default.OnInit
STARTING: MCTS._70_515.Resources._Default.Page_Load
STARTING: MCTS._70_515.Resources._Default.OnLoad
STARTING: MCTS._70_515.Resources.NameBox.Page_Load
STARTING: MCTS._70_515.Resources.NameBox.OnDataBinding
STARTING: MCTS._70_515.Resources.NameBox.get_LastName
STARTING: MCTS._70_515.Resources.NameBox.OnLoad
STARTING: MCTS._70_515.Resources.NameBox.ChangeRequestClicked
STARTING: MCTS._70_515.Resources._Default.ChangeName
STARTING: MCTS._70_515.Resources._Default.OnPreRender
STARTING: MCTS._70_515.Resources.NameBox.OnPreRender
STARTING: MCTS._70_515.Resources.NameBox.OnUnload
STARTING: MCTS._70_515.Resources._Default.OnUnload
Both event handlers in the control and in the page are called after all controls and the page has been loaded and the data is binded too, just before the rendering take place.