Tuesday, 19 July 2011

How to use IList Generic Interface to store data VB.NET


IList Generic Interface represents a collection of objects that can be individually accessed by index.

This example demonstrates how to use IList Generic Interface to store data.
IList Generic Interface represents a collection of objects that can be individually accessed by index.
First, you will need to import the System.Collections.Generic namespace.

Imports System.Collections.Generic;

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

The System.Collections.Generic namespace contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide better type safety and performance than non-generic strongly typed collections.

We use the BindData function to do the work.
We use GridView control to display data. The code as follow:

Private Sub BindData()
Dim userinfos As IList(Of UserInfo) = New List(Of UserInfo)
Dim i As Integer
For i = 0 To 4
Dim info As New UserInfo("username" + i.ToString(), "password" + i.ToString())
userinfos.Add(info)
Next i

Me.GridView1.DataSource = userinfos
Me.GridView1.DataBind()
End Sub

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

I add one userinfo custom class which is used to store data.The code as follow:

Imports System

'/ <summary>
'/ UserInfo
'/ </summary>

Public Class UserInfo
Private _userName As String = String.Empty
Private _password As String = String.Empty

Public Sub New()

End Sub

Public Sub New(ByVal userName As String, ByVal password As String)
Me._userName = userName
Me._password = password
End Sub

Public ReadOnly Property UserName() As String
Get
Return _userName
End Get
End Property

Public ReadOnly Property Password() As String
Get
Return _password
End Get
End Property
End Class

The front end TemplateVB.aspx page looks something like this:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal">
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<Columns>
<asp:BoundField DataField="UserName" HeaderText="UserName" />
<asp:BoundField DataField="Password" HeaderText="Password" />
</Columns>
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<AlternatingRowStyle BackColor="#F7F7F7" />
</asp:GridView>

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

The flow for the code behind page is as follows.

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
BindData()
End If
End Sub

Private Sub BindData()
Dim userinfos As IList(Of UserInfo) = New List(Of UserInfo)
Dim i As Integer
For i = 0 To 4
Dim info As New UserInfo("username" + i.ToString(), "password" + i.ToString())
userinfos.Add(info)
Next i

Me.GridView1.DataSource = userinfos
Me.GridView1.DataBind()
End Sub
End Class

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment