Wednesday, April 4, 2012

ListView and DataPager Controls: Grouping Data with the ListView Control


Introduction


ASP.NET version 3.5 added two new data Web controls to the Toolbox: the ListView and DataPager. As discussed in the first installment of this article series, Displaying Data with the ListView, the ListView control offers the same built-in features found in the GridView, but much finer control over the rendered output. The ListView's output is defined using a variety of templates, and we looked at examples using the control's LayoutTemplate and ItemTemplates. In particular, these examples used a LayoutTemplate that included a placeholder for the ItemTemplate's rendered output.
The ItemTemplate is rendered for each record bound to the ListView control, and is typically referenced in the LayoutTemplate. This approach generates the rendered markup defined in the LayoutTemplate, plus the rendered markup created by the ItemTemplate for each record. This works fine for simple rendering scenarios, but in more complex scenarios we may need to render different formatting markup for different groups of records. For example, imagine that we needed to display a set of records in a three-column HTML <table>. For each record we would want to emit a table cell (<td>), but for every three records we would need to emit a new table row (<tr>). Such customizations can be accomplished declaratively with the ListView control's includes GroupTemplate andGroupItemCount properties.
In this article we will see how to use the GroupTemplate and GroupItemCount properties to instruct the ListView control to render different encasing markup for every n records. We will look at two demos: one that renders records into a series of ordered lists, and another that illustrates how to display data in a multi-column table. Read on to learn more!

Grouping Basics


As we saw in the Displaying Data with the ListView article, the ListView control contains two requisite templates: LayoutTemplate and ItemTemplate. The LayoutTemplate is rendered to generate the ListView control's markup and can contain a reference to the ItemTemplate, which is used to render each record bound to the ListView control. The LayoutTemplate references the ItemTemplate through a server-side control (such as the PlaceHolder control) whose ID is the same as the ListView's ItemPlaceholderID property. (The ItemPlaceholderID property has a default value of "itemPlaceholder".)
Referencing the ItemTemplate directly from the LayoutTemplate works well if the encasing markup for each item is the same, but in several scenarios different encasing markup may need to be introduced every n items. Such customization is possible by defining a GroupTemplate and setting the ListView control's GroupItemCount property to n. Then, instead of referencing the ItemTemplate in the LayoutTemplate, have the LayoutTemplate reference the GroupTemplate, and the GroupTemplate reference the ItemTemplate. Such a setup still has the ItemTemplate rendered for every record bound to the ListView control, but causes the GroupTemplate to be rendered every GroupItemCount number of records.
To better understand how grouping with the ListView control works, let's take the first example we examined in the Displaying Data with the ListView article and extend it to use grouping. The first example illustrated how to display a set of records in an ordered list, and used the following declarative markup for the ListView control:
<asp:ListView ID="..." runat="server" DataSourceID="...">
   <LayoutTemplate>
      <ol>
         <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
      </ol>
   </LayoutTemplate>

   <ItemTemplate>
      <li><%# Eval("columnName") %></li>
   </ItemTemplate>
</asp:ListView>

Imagine, however, that we want to display an ordered list for each group of three records. To accomplish this use the following markup instead:
<asp:ListView ID="ProductList1" runat="server"
   DataSourceID="ProductDataSource"
   GroupItemCount="3" ItemPlaceholderID="itemsGoHere"
   GroupPlaceholderID="groupsGoHere">

   <LayoutTemplate>
      <p>
         <asp:PlaceHolder runat="server" ID="groupsGoHere"></asp:PlaceHolder>
      </p>
   </LayoutTemplate>

   <GroupTemplate>
      <ol>
         <asp:PlaceHolder runat="server" ID="itemsGoHere"></asp:PlaceHolder>
      </ol>
   </GroupTemplate>

   <ItemTemplate>
      <li><%#Eval("ProductName")%></li>
   </ItemTemplate>
</asp:ListView>

The ListView control's declarative markup is nearly the same as in the previous article, but instead of the <ol> element being in the LayoutTemplate, it has been moved to the GroupTemplate. The ItemTemplate has reamined the same. Note, however, that the LayoutTemplate still must be present and now references the GroupLayout. Also note that instead of the default values for the group and item placeholders ("groupPlaceholder" and "itemPlaceholder") I have explicitly changed these values through the ListView control's ItemPlaceHolderID and GroupPlaceholderID properties.
Imagine that the above ListView is bound to an employees database table, and that in the ItemTemplate we were rendering the FullName column within the <li> element. What would the ListView's rendered markup look like?
Well, the ListView would start by rendering it's LayoutTemplate:
<p>
   <asp:PlaceHolder runat="server" ID="groupsGoHere"></asp:PlaceHolder>
</p>

It would then render its GroupTemplate for each group of three records bound to the ListView control. Assuming that there were eight total employees, this would result in the following markup:
<ol>
  <asp:PlaceHolder runat="server" ID="itemsGoHere"></asp:PlaceHolder>
</ol> 


<ol>
  <asp:PlaceHolder runat="server" ID="itemsGoHere"></asp:PlaceHolder>
</ol>

<ol>
  <asp:PlaceHolder runat="server" ID="itemsGoHere"></asp:PlaceHolder>
</ol>

It would then render its ItemTemplate for each record bound to the ListView control. This might result in the following markup:
<li>Scott Mitchell</li>
<li>Sam Smith</li>
<li>Jisun Lee</li>
<li>Andrew Fuller</li>
<li>Edgar Johnson</li>
<li>Ellen Plank</li>
<li>Tito Wald</li>
<li>Layla Clapton</li>

The ItemTemplates' rendered markup would be placed in the appropriate GroupLayout PlaceHolder controls. The net result is the following markup:
<p><ol><li>Scott Mitchell</li>
<li>Sam Smith</li>
<li>Jisun Lee</li>
</ol><ol>
<li>Andrew Fuller</li>
<li>Edgar Johnson</li>
<li>Ellen Plank</li>
</ol>
<ol><li>Tito Wald</li>
<li>Layla Clapton</li>
</ol></p>

Displaying Data in a Multi-Column Table


Displaying records in a multi-column table is a very common scenario, and is one that requires rendering different formatting markup for groups of records. Oftentimes, such formatting is achieved through the use of a multi-column HTML <table>. For example, to display a three-column table, we would render three table cells (<td>) in each table row (<tr>), like so:
<table ...>
   <tr>
      <td>Record 1</td>
      <td>Record 2</td>
      <td>Record 3</td>
   </tr>

   ...

   <tr>
      <td>Record N - 2</td>
      <td>Record N - 1</td>
      <td>Record N</td>
   </tr>
</table>

In order to generate such markup with a ListView control, we need to use a LayoutTemplate that renders the outer <table> and </table> tags, a GroupTemplate that renders the table row elements, and an ItemTemplate that renders each table cell. The following declarative markup illustrates how to display data in a three-column table:
<asp:ListView ID="ProductDataList2" runat="server"
   DataSourceID="..." GroupItemCount="3">
  
   <LayoutTemplate>
      <table>
         <tr>
            <td>
               <table border="0" cellpadding="5">
                  <asp:PlaceHolder runat="server" ID="groupPlaceHolder"></asp:PlaceHolder>
               </table>
            </td>
         </tr>
      </table>
   </LayoutTemplate>

   <GroupTemplate>
      <tr>
         <asp:PlaceHolder runat="server" ID="itemPlaceHolder"></asp:PlaceHolder>
      </tr>
   </GroupTemplate>

   <ItemTemplate>
      <td>
         <h2><%# Eval("ProductName") %></h2>
        
         Price: <%#Eval("UnitPrice", "{0:c}")%><br />
         Category: <%# Eval("CategoryName") %>
      </td>
   </ItemTemplate>
</asp:ListView>

The download available at the end of this article includes a demo that displays the records from the Northwind database's Products table in a three-column table, which yields the following when viewed through a browser.

Conclusion


In this article we examined how to render output batched into a specified group size using the ListView control's GroupTemplate and GroupItemCount property. This template and property are useful in scenarios where the formatting layout needs to change for every n rendered records, such as when displaying a multi-column table. Prior to the ListView control, such grouping required the page developer to write code that would intelligently inject additional formatting markup for every n records. But with the ListView control's GroupTemplate and GroupItemCount property, group formatting is now possible entirely through declarative means.
Happy Programming!
  • By Scott Mitchell

    Further Readings:
  • An Overview of ASP.NET 3.5 and Visual Studio 2008
  • The asp:ListView Control (Part 1)
  • Displaying Data with the ListView
  • Attachments


  • Download the Demo (in ZIP format)
    Reference
  • http://www.4guysfromrolla.com/articles/010208-1.aspx
  • Monday, April 2, 2012

    Linked Server for SQL 2008


    Create Linked Server for SQL 2008


    EXEC master.dbo.sp_addlinkedserver @server = N'GHPMS', @srvproduct=N'GHPMS', @provider=N'[SQL Provider', @datasrc=N'[Server IP]', @catalog=N'[catalog]'

     /* For security reasons the linked server remote logins password is changed with ######## */
    EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'GHPMS',@useself=N'False',@locallogin=NULL,@rmtuser=N'[User Name]',@rmtpassword='[Password]'

    GO

    EXEC master.dbo.sp_serveroption @server=N'[ServerName]', @optname=N'collation compatible', @optvalue=N'false'
    GO

    EXEC master.dbo.sp_serveroption @server= N'[ServerName] ', @optname=N'data access', @optvalue=N'true'
    GO

    EXEC master.dbo.sp_serveroption @server= N'[ServerName] ', @optname=N'dist', @optvalue=N'false'
    GO

    EXEC master.dbo.sp_serveroption @server= N'[ServerName] ', @optname=N'pub', @optvalue=N'false'
    GO

    EXEC master.dbo.sp_serveroption @server= N'[ServerName] ', @optname=N'rpc', @optvalue=N'false'
    GO

    EXEC master.dbo.sp_serveroption @server= N'[ServerName] ', @optname=N'rpc out', @optvalue=N'false'
    GO

    EXEC master.dbo.sp_serveroption @server= N'[ServerName] ', @optname=N'sub', @optvalue=N'false'
    GO

    EXEC master.dbo.sp_serveroption @server= N'[ServerName] ', @optname=N'connect timeout', @optvalue=N'0'
    GO

    EXEC master.dbo.sp_serveroption @server= N'[ServerName] ', @optname=N'collation name', @optvalue=null
    GO

    EXEC master.dbo.sp_serveroption @server= N'[ServerName] ', @optname=N'lazy schema validation', @optvalue=N'false'
    GO

    EXEC master.dbo.sp_serveroption @server= N'[ServerName] ', @optname=N'query timeout', @optvalue=N'0'
    GO

    EXEC master.dbo.sp_serveroption @server= N'[ServerName] ', @optname=N'use remote collation', @optvalue=N'true'
    GO

    EXEC master.dbo.sp_serveroption @server= N'[ServerName] ', @optname=N'remote proc transaction promotion', @optvalue=N'true'
    GO


    Sunday, April 1, 2012


    Now that we have created a simple window base application we will move on to create a simple Tab Bar Application. To start with open XCode again and create a Tab Bar Application like below:
    Tab Bar App
    Then I am going to call mine MyNewTabApp, but feel free to name yours whatever is the most appropriate for you.
    Name Tab App
    Now for this tutorial I am going to use Interface builder a little bit. Click on the MainWindow.xib file. it should look like this afterwards:
    Main Window XIB
    If it might look like this:
    Main Window XIB Alternate
    If this is the case I recommend pulling the small vertical bar on the left of the graph paper to the right until it looks like the first picture. Ok, now that we XCode looking the way we want lets dive into creating the Tab Application. If we build and run the app is it is created for us, we have two tabs and views that are associated with them. The tabs say First View and Second View and you can toggle between them by pressing the tabs at the bottom.
    Basic Tab Bar App
    Now if our goal is to create a simple application that has four pages we will need to add two more pages to our app. To start this control click on the MyNewTabApp folder in the file view. Then click “New File…” choose the UIViewController subclass option and click next.
    UIViewController
    Make sure that the “With XIB for user interface has been chosen” and click next.
    XIB Checked
    I usually put in ThirdViewController as the name especially when creating very simple applications, but feel free to use whatever name you want. Then choose save. Make sure to repeat these steps again for the fourth tab. Now that we have all the view created lets add some stuff to views three and four so we know they are displaying. For view three we will add a Label. Click on the Third View’s xib file and then the view object. Open the Utilities view by clicking on the third View option in the top right of XCode. This will open all the utilities on the right of the IDE. Go down to the objects on the bottom right and drag the label object over to the view. You can make the label say whatever you want. It should look like this:
    Setting Up Label
    For view four we are going to change the background color. To do this click on the Fourth View’s xib file. Then click on the View object. Next make sure that you have the Utilities View open and click on the Attributes Inspector on the right. You should see a section for view and in that section there is an option for background color. Change that to something that stands out. I chose green:
    Setting Up Background Color
    Now go back to the Mainwindow.xib file we are ready to link our Tab Bar Controller with the two new View Controller. Go down to the objects in the bottom right hand corner:
    View Controller Highlighted
    Drag the View Controller object over from the bottom right to the Tab Bar Controller and place it after the second View Controller. Then bring a Tab Bar Item and place it underneath the View Controller Object you just brought over so it looks like this:
    Tab Bar Item
    Now we are ready to link this item with your view. Click on the third View Controller and then click the Identity Inspector on the right. After this load you can choose from the drop down in the Custom Class section the class you used for the third view. Repeat these steps for the fourth view as well. Then Save, Build and Run. The third tab should look like this:
    Third View App
    The fourth tab should be nice and green or whatever color you used like this:
    Fourth View App
    If you want you can edit the names that show up on the tab items by double clicking on them. You can also click on the attributes inspector and the editing the Title text field under bar Item.

    ————————————————————–
    As always, please leave your comments on iPhone applications, development, or any questions you might have.  Lets Get Started!

    reference

    Connecting to a SOAP Objective-C

    http://sudzc.com/
    - It will convert WSDL to SOAP client in Objective-C.

    http://csoap.sourceforge.net/
    - a SOAP library that depends on libxml2 (which is included in the iPhone SDK)



    Hello world for iPhone Developer


    Step 1: Download XCode and the SDK

    The first thing we need to do is to download the development tools namely XCode and the SDK. To download either of these you will need to register for the Apple ID and Apple developer Connection (ADC). Install both these tools, the procedure to do so is fairly trivial so I am not going into details.
     


    Step 2: Create a new Project

    Now we have the tools we need to start coding but before we do we need to create a new project.Select File > New Project orApple Key + Shift + N to bring up the new project menu. Select the Applications item of theIPhone OS section from the menu on the left, and select View Based Application from the icons on the right. When prompted enter a project name, I have used HelloWorld in the sample code.


    When you create a project you will see a window similar to the one shown in the image on the right. There are four files in the Classes package;
    HelloWorldAppDelegate.h
    HelloWorldAppDelegate.m
    HelloWorldViewController.h
    HelloWorldViewController.m

    We do not need to change the HelloWorldAppDelegate but just for information I would like to highlight some important parts of the auto-generated code.
    The delegate header file contains a reference to a UIWindow object (Line 14) which controls all the user interaction for this application and will manage all other interface components. It also contains a reference to HelloWorldViewController (Line 15) which we will see later manages our one sample view.

    In the source file for the application delegate (HelloWorldAppDelegate.m) we find an auto generatedapplicationDidFinishLaunching method. This, as the name suggests, invoked when the application has been loaded and in this method we will add our HelloWorldViewController object to the UIWindow and make it visible;
    22    [window addSubview:viewController.view];
    23    [window makeKeyAndVisible];  

    The IPhone SDK UI follows the Model View Controller (MVC) design pattern and hence each view  has a corrsponding View Controller Object. XCode has automatically created a view for us, (its defined in the HelloWorldViewController.xib file you can see it by double clicking it in the side menu) and linked it to HelloWorldViewController. We now need to add controls to the view handle the generated events. Note we can add controls via a GUI interface called the Interface Builder but I am using the programmatic way for this tutorial. 


    Step 3: Adding Controls and Handler to header file


    Open the HelloWorldViewController.h file and within the curly braces delinting the HelloWorldViewController interface declaration add the following lines;

    IBOutlet UIButton *button;
    IBOutlet UILabel *label;
    IBOutlet UIView *myView;


    We are defining a view to display and a button and label to go in the view. After the curly braces add a method declaration to accept the click event of the button and also properties to access the  UI elements; myButton, myLabel and myView. Your entire code should look something like this:

    1    #import <UIKit/UIKit.h>
    2
    3    @interface MyHelloWorldViewController : UIViewController 
    4    {
    5
    6        IBOutlet UIButton *button;
    7        IBOutlet UILabel *label;
    8        IBOutlet UIView *myView;
    9    }
    10
    11    -(IBAction)handleEvent:(id)sender;
    12    @property (nonatomic, retain) UIButton *button;
    13    @property (nonatomic, retain) UILabel *label;
    15    @property (nonatomic, retain) UIView *myView;
    16
    17    @end


    Step 4: Adding Controls


    First add a synthesize the three UI elements to create the getter and setters. Add the following two lines of code after the @implementation HelloWorldViewController command in line 11 of HelloWorldViewController.m file. 

    @synthesize button;
    @synthesize label;
    @synthesize myView;

    Now find the comment saying "Implement loadView if you want to create a view hierarchy programmatically." and un-comment the loadView method that follows this line. Add the following lines to the method to create a button and label;
    1    - (void)loadView 
    2    {
    3    // create and configure the view
    4    CGRect cgRct = CGRectMake(0.0, 0.0, 480, 320); //define size and position of view 
    5    myView = [[UIView alloc] initWithFrame:cgRct]; //initialize the view    
    6    myView.autoresizesSubviews = YES;              //allow it to tweak size of elements in view 
    7    self.view = myView;        

    8    
    // create a UIButton (UIButtonTypeRoundedRect) and play arround with settings
    9    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];    
    10   button.frame = CGRectMake(100, 100, 100, 50); // size and position of button
    11   [button setTitle:@"Add" forState:UIControlStateNormal];
    12  button.backgroundColor = [UIColor clearColor];
    13  button.adjustsImageWhenHighlighted = YES;     

    14  
    //Add action handler and set current class as target
    15  [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];

    16  //Display Button
    17  [self.view addSubview:button];
            
    18  //create a label
    19  cgRct = CGRectMake(100, 170, 100, 50); //define size and position of label
    20  label = [[UILabel alloc] initWithFrame:cgRct];
    21  label.text = @"Hello World";

    22  //Display Label
    23  [self.view addSubview:label];
    24  }


    Click the Build and Go button to check your interface is drawn correctly It should look something like to screen shot below.
     


    Step 5: Handling Actions



    We have already told the button to send an event (action) to the current class (self) in case of anUIControlEventTouchUpInside event but there is no handler defined to accept the event. We define a handler method in the class by adding the method shown below. It is doing two things, changing the text property of the label and adding a message to the log file.  
     
    1    -(void)action:(id)sender
    2    {
    3        NSLog(@"UIButton was clicked");
    4        label.text = @"Bye World";
    5    }


    Now click the Build and Go button again and click the button. The text of the label should change.


    Reference
    -http://www.tuaw.com/2009/04/27/iphone-dev-101-the-hello-world-app/