Thursday, September 25, 2008

Some good practices of creating custom .net user control

1. No data binding or logic should be contained in the code.

2. There should be some property to data bind or access the control.

3. JavaScript should be place inside in a .js file because it makes it very easy to make global JavaScript changes to the application.

4. As a web page may contain several instance of the custom control, the JavaScript should not be included for each instance. So for including the JavaScript just for once per page the following code can written in the Page_Load(object sender, EventArgs e) method:

if(!this.Page.ClientScript.IsClientScriptBlockRegistered(keyClientBlock))
{
this.Page.ClientScript.
RegisterClientScriptBlock(this.Page.GetType(),
"keyClientBlock",jScript);
}
Here jScript string contains the JavaScript code.

5. If you need to execute some this javascript to initialize some tool then use RegisterStartupScript() method. The parameter list of this method is same as RegisterClientScriptBlock(). The difference between RegisterStartupScript() & RegisterClientScriptBlock() is that the first one puts the script at the bottom of the ASP.NET page and the latter puts that at the top of the page.

Tuesday, September 23, 2008

Ajax Update panel in DotNetNuke 4.0 or upper

The DotNetNuke Framework cannot assume that the web server it is running on has ASP.NET AJAX installed. In order to handle this a new class was created in DotNetNuke.Framework.AJAX which provides methods that developers can use to leverage AJAX in their solutions. For detail about this the following page can be visited -

Creating A DotNetNuke Module using ASP.NET AJAX

But without following the instructions in this page, you can add ajax support in your dotnetnuke module easily. Follow the instruction step by step:

1. instead of ajax update panel add an asp:panel in the module.

2. override the - void OnInit(EventArgs e) method of module i.e. asp.net custom control.

3. In the override OnInit() method register DotNetNuke Ajax Script Manager by calling the method DotNetNuke.Framework.AJAX.RegisterScriptManager().

4 . Then add your asp:panel for ajax support by calling method DotNetNuke.Framework.AJAX.WrapUpdatePanelControl(#asppanelID#, true);

5. You can also allow postback of this module for a specific control in the asp:panel by calling DotNetNuke.Framework.AJAX.RegisterPostBackControl(#aspPostBackEnableControl#).

Sample code block:

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
try
{
if (DotNetNuke.Framework.AJAX.IsInstalled())
{
DotNetNuke.Framework.AJAX.RegisterScriptManager();
DotNetNuke.Framework.AJAX.
WrapUpdatePanelControl(this.pnlUpdate, true);
DotNetNuke.Framework.AJAX.
RegisterPostBackControl(this.btnSubmit);
}
}
catch (Exception exp)
{
//Step if ajax is not supported.
}
}