Thursday, December 16, 2010

List View Control with Data Pager

ASp.net 3.5 introduced a new control Data Pager that works only with List View control for paging.It can't be used with other data controls. Data Pager is customizable we can create paging in list view using this control.We can place it anywhere on page.We can create simple next,previous or google style paging with the help of this control.

Steps to use DataPager control:-

1. Place list view on page and bind it with data.

2. Add Datapager on page.

3. Set Datapager property PagedControlID="" (It'll be the id of list view)

4. Add Page Size "or PageSize='5'"

5. Select Fields property and select paging type from available fields(Next/Previous Pager field, Numeric Pager Field or Template Pager Field)

6. so that's it.


HTML Code














Class File Code












Thanks & Regards,
Vikas Sharma

List View Control - Group Template

You can group a number of records using group template. Here I am displaying the records in tabular format 3 records per group.


Steps to use Group Template
1. First create a Layout Template and add a place holder in it.
2. Add a Group Template and add table in it as follow
3. Create Item Template and all all fields which u want to display
4. Add Group Separator Template to separate groups
5. In List view properties set below


GroupItemCount:- Set it to number of records you want to display as coulumn.for ex:- If you set it to 2 then you can view 2 records per row in 2 columns.

GroupPlaceHolderId:- Set Layout Template PlaceHolder Id to this property.

ItemPlaceHolderId:- Set it to display the items of Item Template.In our case it'll be group placeholder id ie:- itemplaceholder. By default the id for Itemplaceholder is "itemplaceholder".If you used this id then you dont need to set ItemPlaceholderId for Item



ASPX Code















Class File Code











Output







That's it. Using above steps you can group your records. In next post i'll add code for data Manipulation in List View control.


Thanks & Regards,
Vikas Sharma

ListView Control in .Net 3.5

This is a powerful and most flexible databound control introduced in Asp.net 3.5. It has a lots of awesome features as compared to other databound controls.This controls contains functionality combined of all databound controls(DataGrid,GridView,DataList,Repeater).The most important feature of this control is that like the Repeater control it provides much more control over the layout of the output.

Templates:- List view control contain 11 Templates out of which 1- 6 templates are already existing in other databound controls. 7 - 11 are new templates introduced in this control.

Templates in List View
1. Item Template
2. Alternating Item Template
3. Edit Item Template
4. Insert Item Template
5. Selected Item Template
6. Empty Item Template
7. Layout Template
8. Group Template
9. Group Separator Template
10. Item Separator Template
11. Empty Data Template

compulsory Templates while using ListView:-
When we use list view control then it must contains the following templates rest templates are optional.

1. Layout Template

2. Item Template

How to bind a ListView Control

HTML Code













Class file Code (I bind ListView using LINQ)










This is just an Introduction to List View Control Soon I'll add more details about List View features and Functionalities.



Thanks & Regards,
Vikas Sharma


Register User Controls in Web Config file

Before we start let us first understand User Controls and their advantage.


User controls:- A user control is a kind of composite control that works much like an ASP.NET Web page—you can add existing Web server controls and markup to a user control, and define properties and methods for the control. You can then embed them in ASP.NET Web pages, where they act as a unit. These user controls are reusable and we can add them on as many pages we want.

Now when we use same user control on multiple pages, we always use to register them on every page.we generally do something like this:-













But But But........ We are now in a new world of .net Technologies. So we can now do this in a much cleaner and easier manner in ASP.NET 2.0. Instead of duplicating them on all your pages, just declare them once within the new pages->controls section with the web.config file


















Thanks,
Vikas Sharma

Aspx Page Life Cycle

Page Life Cycle can be defined as steps & Events fired between the page request & rendering of page on client side.

Below are the steps in Page Life Cycle:-

P - Page Request

S - Start

I - Initialization

L - Load

V - Validate

E - Event Handling

R - Rendering

U - Unload

D- Dispose



1. Start:- This is where page properties such as Request, Response, IsPostback & UI Culture are set.If we need to access or override behaviour for this step then we can use Preinit() method to create or recreate dynamic controls, set a master page or a theme or read or set profile values. If the request is a Postback then the value of the controls have not yet been restored from view state. If you set a control property at this stage then its value might be overwritten in the next event.

Event:- PreInit()

2. Initialize:- Here themes are applied & unique Ids are generated & set for controls.We can have access to Init(), InitComplete() & Preload() method in this stage.

Event:-
Init():- This event is raised after all controls have been initialized & any skin settings have been applied. We can use this event to read or initialize control properties.

Init Complete():- This event is raised by Page object. We can use this event for processing tasks that require all Initialization be complete.

PreLoad():- This can be use if we need to perform processing on page or control before the Load event.After the page raised this event. It loads viewstate for itself & all controls & then processes any postback data included with Request Instance.

3. Load:- In this stage controls are loaded with information retrieved from view & control state.The Onload() is the event method that fires during this stage.

Event:-
OnLoad():- We can set properties for server controls, Request Query String & establish database connection in this event.

4. Validation:- If we have events that needs validation then they are validate here & we can check there IsValid property.

Event:-
Validate():- It contains one overloaded method that accepts a validation group string.

5. Event Handling:- Controls event handling occurs in this stage.Click, SelectedIndexchanged etc are applied to server controls here & if page is posted back then event handlers fired by the controls.

Event:-
Load Complete:- This event fires when all controls for the page have been loaded.

PreRender:- The page object will call EnsureChildControls for each control & finally for the page. Any data bound control that has a DataSourceId will call its databind method.This event occurs for each control on page.view state will save for all controls & page.

SaveStateComplete:- This event fires after the view state saved for the page & control.

6. Render:- This method is called by page on each control.It writes out the HTML markup for the control to the browser.For our custom controls we can override this method to change the markup of control. If we have override the default asp.net control and we don't want to provide it custom markup then it'll render with the default markup.

7. Unload:- It occurs first for all controls and then for the page.At this stage all controls have been rendered to the output stream & cannot be changed.During this event any attempt to access the response stream will result in an exception.This event is mainly for cleanup routines like closing open database connections, Open file stream, event logging & other tasks.

So basic list of events are as follow:-
1. PreInit()
2. Init()
3. OnInitComplete()
4. Preload()
5. Load()
6. Control Events()
7. LoadComplete()
8. PreRender()
9. PreRenderComplete()
10. Save State Complete()
11. Render()
12. Unload()
13. Dispose()


Thanks & Regards,
Vikas Sharma

Control Aspx Event on Page Postback

Many of us facec a problem in asp.net pages that is last event fired when user refresh the page.for example if user is adding a record and after adding the record he hits "F5" to refresh the page then the last add event will fire again and insert duplicate record.Below is to handle it.

-----------------------------------------------------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["value"] = Server.UrlEncode(System.DateTime.Now.ToString());
}
}


protected void Page_PreRender(object sender, EventArgs e)
{

ViewState["value"] = Session["value"];

}

protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Session["value"].ToString() == ViewState["value"].ToString())
{
lblMsg.Text = "Button clicked";
Session["value"] = Server.UrlEncode(System.DateTime.Now.ToString());
}
else
{
lblMsg.Text = "Page refreshed";
}
}

-----------------------------------------------------------------------------------



In above example we use a viewstate object and a session object to check if page control event or page refresh event fired. First we set the session object "update" as system datetime on page load when page is first time loaded.after that we set the viewstate value on page_prerender event by passing session object value in it.Now on button click we check if both session and viewstate object values are same. If they are same it means button click fired but if they do'nt match then it means that page is refreshed.The magic here is that whenever page refresh event fired if did'nt refresh the viewstate value but whenever button event fire it return the updated value of viewstate.So when button clicked both value matched but if page refresh then they dnt match.


Thanks & Regards,
Vikas Sharma