Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Posts Tagged ‘Datalist’

Differences between Datagrid, Datalist and Repeater?

Posted by Viral Sarvaiya on July 13, 2009

1. Datagrid has paging while Datalist doesnt.
2. Datalist has a property called repeat. Direction = vertical/horizontal. (This is of great help in designing layouts). This is not there in Datagrid.
3. A repeater is used when more intimate control over html generation is required.
4. When only checkboxes/radiobuttons are repeatedly served then a checkboxlist or radiobuttonlist are used as they involve fewer overheads than a Datagrid.
The Repeater repeats a chunk of HTML you write, it has the least functionality of the three. DataList is the next step up from a Repeater; accept you have very little control over the HTML that the control renders. DataList is the first of the three controls that allow you Repeat-Columns horizontally or vertically. Finally, the DataGrid is the motherload. However, instead of working on a row-by-row basis, you’re working on a column-by-column basis. DataGrid caters to sorting and has basic paging for your disposal. Again you have little contro, over the HTML. NOTE: DataList and DataGrid both render as HTML tables by default.
Out of the 3 controls, I use the Repeater the most due to its flexibility w/ HTML. Creating a Pagination scheme isn’t that hard, so I rarely if ever use a DataGrid. Occasionally I like using a DataList because it allows me to easily list out my records in rows of three for instance.

Datagrid is most restrictive as regards to customization followed by DataList and finally Repeater is the most customizable.

Datagrid has built in paging sorting and editing capabilities which are not there with the other two controls. So if you want users to sort / page / edit data datagrid is the natural choice.

DataList and repeater have better performance than datagrid. So if performance is a major concern for example a site with large number of concurrent visitors then you could think of datalist or repeater.

Repeater is the most customizable. It allows you to create structures like nested lists for example.

A datagrid row displays one record from the data source while a datalist row can display more than one records (set by RepeatColumns property)

Datagrid and Datalist are derived from WebControl while Repeater is not and so does not have the stylistic properties of web controls.

All are similar in that they have a datasource property and ItemCreated ItemDataBound and ItemCommand events.

Posted in ASP.NET | Tagged: , , | Leave a Comment »

Datalist Custom Paging

Posted by Viral Sarvaiya on March 13, 2009

In this article i will explain a method for providing custom paging for datalist or repeater.

As you know the datalist is a very powerful control with one drawback that it does not have built-in paging capability, a feature the DataGrid offers. to provide paging to datalist or repeater we can either use “PagedDataSource” class, found in the System.Web.UI.WebControls namespace for auto paging like the datagrid or implement custom paging functionality.

But I describe new things for datalist paging, i use plasecontrol for paging as below.

Step 1 :  Make a Default.aspx page




<asp:PlaceHolder ID=”phtopsort” runat=”server”></asp:PlaceHolder>

<asp:DataList ID=”dlFabricsort” runat=”server” DataKeyField=”Fabricid” ShowFooter=”false” EnableViewState=”false”
ShowHeader=”false” RepeatColumns=”3″ RepeatDirection=”Horizontal” HorizontalAlign=”left”
CellPadding=”0″ CellSpacing=”0″ Width=”93%” BackColor=”#F4F1E6″>

<ItemStyle VerticalAlign=”Top” Height=”100%” HorizontalAlign=”Left”/>
<table width=”100%”>

<asp:Label ID=”Label1″ runat=”server” Text ='<%# container.dataitem(“fabricname”)%>’></asp:Label>



Step 2 : In Default.aspx.vb

Dim total As Integer

Dim totalpages As Integer

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack = False Then

If Request.QueryString(“p”) = Nothing Then
page1 = 1
ElseIf Request.QueryString(“p”) <= 0 Then
page1 = 1
ElseIf Request.QueryString(“p”) = “” Then
page1 = 1
ElseIf Request.QueryString(“p”) <> “” Then
page1 = Request.QueryString(“p”)
End If


End If
End Sub

sub datalist_sortfabric()

Dim dssort As New DataSet
Dim dssort1 As New DataSet

dssort = objfabric.SelectSortFabric()  ‘ Select full Dataset

If dssort.Tables(0).Rows.Count > 0 Then

total = dssort.Tables(0).Rows.Count
size = 15   ‘total recordper page

Dim tp As Double = total / size
Dim arr As Array = tp.ToString.Split(“.”)
totalpages = arr(0)
If total Mod size <> 0 Then
totalpages = totalpages + 1
End If

dssort1 = objfabric.SelectSortFabricPaging((page1 – 1) * size, size)

record.Value = dssort.Tables(0).Rows.Count

dlFabricsort.DataSource = dssort1


end if

end sub

sub dynamic_paging()

Dim str As String = “”
Dim i As Integer


If page1 = 1 Then
str += “”
str += “<a href=’Default.aspx?p=1′ class=’link12′>First</a>&nbsp;
End If


If page1 = 1 Then
str += “”
str += “<a href=’Default.aspx?p=” & page1 – 1 & “‘ class=’link12’>Previous</a> &nbsp; ”
End If

For i = 0 To totalpages – 1

Dim tmp As String = “”
If i = 0 Then
If i + 1 = page1 Then
tmp = “<a href=’Default.aspx?p=” & i + 1 & “‘ class=’link12′>” & i + 1 & “</a>”
tmp = “<a href=’Default.aspx?p=” & i + 1 & “‘ class=’link12′>” & i + 1 & “</a>”
End If
If i + 1 = page1 Then
tmp = ” | <strong> <a href=’Default.aspx?p=” & i + 1 & “‘ class=’link12′>” & i + 1 & “</a></strong>”
tmp = ” | <a href=’Default.aspx?p=” & i + 1 & “‘ class=’link12’>” & i + 1 & “</a>”
End If

End If

If i >= page1 – 6 And i <= page1 + 4 Then
str += tmp
str += “”
End If

If page1 = totalpages Then
str += “”
str += “&nbsp;&nbsp;&nbsp;<a href=’Default.aspx?p=” & page1 + 1 & “‘ class=’link12’>Next</a>”
End If


If page1 = totalpages Then
str += “”
str += “&nbsp;&nbsp;&nbsp;<a href=’Default.aspx?p=” & totalpages & “‘ class=’link12’>Last</a>”
End If

Dim tab As Table = New Table
tab.CellSpacing = 0
tab.CellPadding = 0
tab.BorderWidth = Unit.Point(0)

Dim tabrow As TableRow = New TableRow

Dim tabcell As TableCell = New TableCell
tabcell.VerticalAlign = VerticalAlign.Middle
tabcell.HorizontalAlign = HorizontalAlign.Center

tabcell.Text = str

end sub

here SelectSortFabricPaging() function contains  page index and page size means when your dataAdapter fills value

objAdp.Fill(ds, pindex, psize, “fabrics”)

here  ds is dataset, pindex is current page index, psize is size of page, and “fabreics” is name of table from you get the records.

And last more one things to that when we build a string for placeholder, we write a page name (default.aspx) if your page contains some querystring then write it there.

Posted in ASP.NET | Tagged: , , , | Leave a Comment »

%d bloggers like this: