Peppy Custom Panel Control

This post is also dedicated to my companies prestigious client.

My team was facing a very peculiar problem with this peppy custom panel container control which one of my team member created from Control class (i.e. System.Web.UI.Control) instead of Panel class (i.e. System.Web.UI.WebControls.Panel).
The reason for choice was that the default markup generated by the Panel class was in the form of tables and since my client deals in Mobile Web portal and the browsers of devices are very very harsh on developers. You make a small mistake or a small smarteness will cost you a hard kick on your butt…i could use words which are even worse to tell you how sensitive these mobile browsers are 🙂  So, i need the markup in the form of “DIVS” instead of “TABLES”.

So, in order to generate the markup in the form of DIVS my friend created it using the Control class and Overrided the CreateChildControl method in it and finally, injected the necessary DIV to create a parent control with some attributes in the form of CSS classes passed by the consumer page or user control using this controls property holding the attributes in the form of NameValue pair.

After this thoughtfull yet messy code(i am sure this friend of mine will definately kill me after reading this article 🙂 ) it almost worked fine in every situation and just then it showed up with some bright colors almost at the verge of development cycle in other words, it broke into millions of pieces. 🙁

So the pseudocode goes like this:
==================================
public NameValuePair[] CustomAttributes
{
 //Contains the attributes passed by the consumer page/user control
}
 
protected override CreateChildControl()
{
 //Created the required div over here
 
 //looped through the CustomAttributes and added to the div above
 // looped through "this.Controls" collection 
 //and added each control to the div above(This is the real mess) 
}

 The above control works fine as long as this parent control contains static controls like: (Label, textboxes etc.). The control struggles when a control like CheckBoxList is placed under its hood. Obviously now the user will try to gather the selected checkboxes values and just then the control breaks. On PostBack when the page tries to gather the checkbox values by looping through this.items (i.e. ListItems) the count of ListItems comes out to be 0 (yes, ZERO).

Oops, forgot to tell you the CheckBoxList is also a custom control derived from Asp.Net CheckBoxList. This control was also overriden to get the markup in the form of DIVS. So, this.Items in last para will give you the ListItems of checkboxlist.

So, in a nut shell these are a kind of custom adapters written for adaptive rendering of the controls depending on the device type.

 Coming back to the point, I can’t tell you the exact reason for this count zero, I think we had to do something related to the viewstate while adding the individual control to the parent DIV in the CreateChildControl method.

Alright enough of problem statement, now lets come straight to the solution, After a lot of ignorance today i took up the task of clearing this mess. Poor this Custom Panel here i come yoooo man 🙂 hahaha

Here comes the magic code….

namespace custom.ServerControls
{
    [ParseChildren(false),
      ToolboxData("<{0}:CustomPanel runat=\"server\"></{0}:CustomPanel>")]
    public class CustomPanel : Panel
    {          
        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.WriteLine("<div style=\"border:5px solid black;\">");
             //Its safe to write your custom code here
             writer.WriteLine(”<img src=”"your icon image source”" />”)
   
            //Calling base class(i.e. System.Web.UI.WebControls.Panel)
            //RenderContents method
            base.RenderContents(writer);
            writer.WriteLine("</div>");
        }
    }
}

There is the main part of the code! We simply override the RenderContents method! What this does instead of jumping ahead and just inputing the panel controls into the panel control, we instead snatch it before it finishes and add some of our own custom code!

It’s a simple htmltextwriter object so it’s really easy to use.

You take writer and use the writeline method to add the begining code. Don’t worry this won’t edit the contents of writer. I know it seems odd a little but writeline doesn’t effect the htmltextwriter at all, it just injects code into the page where the panel control is at.

So by this solution two of most significant problems are solved:

  1. My friend don’t have to create the custom panel from Control class as done above,
  2. The view state issue of the child controls will be handled by the panel class along with the EnableViewState = “true” property of the page,
  3. Since Control class does not provide Attributes propert for adding custom attributes to the panel like: CSS class etc. for which my friend has created the NameValuePair array property in the pseudocode above. As the current control is derived directly from Panel class the Attributes property will be available directly.  🙂

Cheers !!! Enjoy!!! 🙂

kick it on DotNetKicks.com

Shout it

pimp it

Intoduction to JAR,EAR and WAR files in java

JAR is java arcihve file.It contain all class file,image,sound and other files which will needed in whole application. Computer users can create or extract JAR files using the jar command that comes with the JDK. They can also use zip tools. The JavaTM Archive (JAR) file format enables you to bundle multiple files into a single archive file. jar was designed mainly to facilitate the packaging of java applets or applications into a single archive.

jar cf jar-file input-file(s)

The options and arguments used in this command are:
• Use c option  to create a JAR file.
• jar-file is the name of your  JAR file. You can use any filename for a JAR file. By convention, JAR filenames are given a .jar extension,it is mandatory any way.

The command for viewing the contents of a JAR file is:

jar tf jar-file
An Example
Let’s use the Jar tool to list the contents of the vinay.jar file we created in the previous section:
jar tf vinay.jar
EAR -An Enterprise Archive file represents a J2EE application that can be deployed in a Web Sphere application server. EAR files are standard Java archive files and have the file extension .ear. EAR file contain ejb, web or application client module. ear file is complete j2ee application file that contain all(jar +war)
WAR -Web Archive (WAR) file is a Java archive file used to store jsp,servlets,classes,meta data information,images and
Sound and tag libararies etc. Its standard file extension is .war. WAR files are used to package Web modules. A WAR file is for a Web application deployed to a servlet/jsp engine.

In shot we can say
EAR = WAR(Web module) + JAR(can be EJB module or application client module)

for more information check this one -http://java.sun.com/docs/books/tutorial/deployment/jar/build.html

Introduction to JSR-227

JSR-227 defines a standard way for tools to implement the interactions between user interfaces and services, doing this in a way that will work for any user interface and any service technology. MVC is application design pattern in this.Primary supported by Sun+oracle. JSR 227 defines an
abstraction mechanism. We are accessing data from JavaBeans, EJB, JDO or POJO in order to reterive our view. In the world of MVC applications the View and Controller layers need to interact with the Model layer. Until this JSR came along, the developer had to learn the specific API for the technology that implemented its Model layer in order to build his View and Controller layers.
With JSR 227 there is a single standard API that works with any implementation of a Model layer – regardless of the implementing technology which can be EJB, SDO, XML, JavaBeans, etc – as well as with any implementation of View layer, such as JSP, JSF, Struts, Swing or any new technology that might pop up. This will be beneficial for two group one is Developer who design UI interface. The other group is providers of business services. For example I might be a business service provider with an innovative way to implement business service. Now all I need to do is provide a data control implementation to my business service – any tool that supports JSR-227 will be able to use my business services in a transparent way.

JSR-227 influence the future of application development :-
JSR-227 as an enabling standard for Service Oriented Architecture (SOA). The concept of SOA is that you can pick up services from anywhere and use them in your application. What JSR-227 will enable you to do is ignore the specific implementation of the service and easily bind user interfaces to this service. This helps create the needed separation between service developers and application developers.

More information you can find on
:-
1.www.oracle.com
2. http://web1.jcp.org/en/jsr/detail?id=227(JSR 227 home page)

References
http://www.oracle.com/technology/tech/java/newsletter/articles/jsr227_interview.html

pimp it