Friday, February 27, 2009

Usage of keyword module?

During looking in the VB.NET code of my new Job I saw sometimes the keyword module. This keyword doesnot exist in C#.NET. The meaning of this keyword is the same as a class only all the members and methods are default shared. So the developer can become lazy;)

So my advise is to minimize the usage of the keyword module and use always the keyword class.

Friday, February 20, 2009

Some introduction links for Unity

From an previous colleague I have received the following links with some information about unity:


Reageer op externe events in plaats van zelf de touwtjes in de hand te houden. or the English variant “Don’t call us we will call you”

Thursday, February 19, 2009

How to nest web user controls dynamically?

When you use nested Web User Controls and you load them with the LoadControl method, add these controls in PlaceHolder.Controls collection (or other controls except the Page class)


Example code

wucMain.aspx.cs


protected void Page_Load(object sender, EventArgs e)
{
wucSub lMain = (wucSub)Page.LoadControl("Windows/wucSub.ascx");
Controls.Add(lMain);
}

wucMain.aspx
<strong>Main</strong><br /><asp:ListBox runat="server" ID="lstItems" />


wucSub.aspx.cs

no code behind



wucSub.aspx
<strong>Main</strong><br /><asp:ListBox runat="server" ID="lstItems" />


Test.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
wucMain1 lMain = (wucMain1)Page.LoadControl("Windows/wucMain.ascx");
Controls.Add(lMain);
}


Test.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>

When you build and run these pages, you will get the following error message
"Control 'ctl02_lstItems' of type 'ListBox' must be placed inside a form tag with runat=server."



How can you solve this?

Do not use Page.Controls.Add or Controls.Addd but specify to which Controls collection these these controls must be added and use there a PlaceHolder (panel or other control). So the correct implemention will be


wucMain.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
wucSub lMain = (wucSub)Page.LoadControl("Windows/wucSub.ascx");
plhSub.Controls.Add(lMain);
}


wucMain.aspx
<strong>Main</strong><br /><asp:ListBox runat="server" ID="lstItems" /><asp:PlaceHolder runat="server" ID="plhSub" />


wucSub.aspx.cs

no code behind



wucSub.aspx
<strong>Main</strong><br /><asp:ListBox runat="server" ID="lstItems" />

Test.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
wucMain1 lMain = (wucMain1)Page.LoadControl("Windows/wucMain.ascx");
plhMain.Controls.Add(lMain);
}

Test.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder runat="server" ID="plhMain" />
</div>
</form>
</body>
</html>