Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Security.Permissions
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.Design
Imports System.Web.UI.Design.WebControls
Imports System.ComponentModel
Imports System.ComponentModel.Design
Namespace ASPNet.Design.Samples_VB
<Designer(GetType(CustomDataSourceDesigner)), _
ToolboxData("<{0}:CustomDataSource runat=""server""></{0}:CustomDataSource>")> _
Public Class CustomDataSource
Inherits ObjectDataSource
Dim _defaultViewName As String = "BookList"
Dim _view As ObjectDataSourceView = Nothing
Public Sub New()
MyBase.New()
End Sub
' Gets a view by name
Protected Overrides Function GetView(ByVal viewName As String) As DataSourceView
' This data source only allows one view
If Not (viewName.Equals(_defaultViewName)) Then
Return Nothing
ElseIf IsNothing(_view) Then
_view = New CustomDataSourceView(Me, _
_defaultViewName, HttpContext.Current)
End If
Return _view
End Function
' Gets a list of view names for this class
Protected Overrides Function GetViewNames() As ICollection
Dim ar As New ArrayList()
ar.Add(_defaultViewName)
Return CType(ar, ICollection)
End Function
End Class
' The runtime data source view
Public Class CustomDataSourceView
Inherits ObjectDataSourceView
Dim _data As ArrayList = Nothing
Public Sub New(ByVal owner As CustomDataSource, ByVal viewName As String, ByVal context As HttpContext)
MyBase.New(owner, viewName, context)
owner.SelectCountMethod = "GetCount"
End Sub
' This method would typically get a set of live data
' rather than create some dummy data
Protected Overrides Function ExecuteSelect(ByVal arguments As DataSourceSelectArguments) As System.Collections.IEnumerable
If (IsNothing(_data)) Then
_data = New ArrayList()
_data.Add(New BookItem("ID_1", "Runtime Title 01"))
_data.Add(New BookItem("ID_2", "Runtime Title 02"))
_data.Add(New BookItem("ID_3", "Runtime Title 03"))
End If
Return CType(_data, IEnumerable)
End Function
' Allow getting the record count
Public Overrides ReadOnly Property CanRetrieveTotalRowCount() As Boolean
Get
Return True
End Get
End Property
' Returns the number of records in the current set of data
Public ReadOnly Property GetCount() As Integer
Get
If IsNothing(_data) Then
Return 0
Else
Return _data.Count
End If
End Get
End Property
' Do not allow deletions
Public Overrides ReadOnly Property CanDelete() As Boolean
Get
Return False
End Get
End Property
' Do not allow insertions
Public Overrides ReadOnly Property CanInsert() As Boolean
Get
Return False
End Get
End Property
' Do not allow paging
Public Overrides ReadOnly Property CanPage() As Boolean
Get
Return False
End Get
End Property
' Do not allow sorting
Public Overrides ReadOnly Property CanSort() As Boolean
Get
Return False
End Get
End Property
' Do not allow updating
Public Overrides ReadOnly Property CanUpdate() As Boolean
Get
Return False
End Get
End Property
End Class
' A class to define a record of the data
Public Class BookItem
Private _id As String
Private _title As String
Public Sub New(ByVal id As String, ByVal title As String)
_id = id
_title = title
End Sub
Public ReadOnly Property ID() As String
Get
Return _id
End Get
End Property
Public Property Title() As String
Get
Return _title
End Get
Set(ByVal value As String)
_title = value
End Set
End Property
End Class
' Custom designer for the CustomDataSource control.
Public Class CustomDataSourceDesigner
Inherits DataSourceDesigner
Private _control As CustomDataSource = Nothing
Private _defaultViewName As String = "BookList"
Private _view As CustomDesignDataSourceView = Nothing
Public Overrides Sub Initialize(ByVal cmponent As IComponent)
MyBase.Initialize(cmponent)
_control = CType(cmponent, CustomDataSource)
End Sub
' Get a view
Public Overrides Function GetView(ByVal viewName As String) As DesignerDataSourceView
If Not (viewName.Equals(_defaultViewName)) Then
Return Nothing
ElseIf IsNothing(_view) Then
_view = New CustomDesignDataSourceView(Me, _
_defaultViewName)
End If
Return _view
End Function
' Get a list of view names
Public Overrides Function GetViewNames() As String()
Return New String() {"BookList"}
End Function
' Don't allow refreshing the schema
Public Overrides ReadOnly Property CanRefreshSchema() As Boolean
Get
Return False
End Get
End Property
' Do not allow resizing
Public Overrides ReadOnly Property AllowResize() As Boolean
Get
Return False
End Get
End Property
End Class
' A design-time data source view
Public Class CustomDesignDataSourceView
Inherits DesignerDataSourceView
Private _data As ArrayList = Nothing
Public Sub New(ByVal owner As CustomDataSourceDesigner, ByVal viewName As String)
MyBase.New(owner, viewName)
End Sub
' Get data for design-time display
Public Overrides Function GetDesignTimeData( _
ByVal minimumRows As Integer, _
ByRef isSampleData As Boolean) As IEnumerable
If IsNothing(_data) Then
' Create a set of design-time fake data
_data = New ArrayList()
Dim i As Integer
For i = 1 To minimumRows
_data.Add(New BookItem("ID_" & i.ToString(), _
"Design-Time Title 0" & i.ToString()))
Next
End If
isSampleData = True
Return CType(_data, IEnumerable)
End Function
Public Overrides ReadOnly Property Schema() As IDataSourceViewSchema
Get
Return New BookListViewSchema()
End Get
End Property
' Allow getting the record count
Public Overrides ReadOnly Property CanRetrieveTotalRowCount() As Boolean
Get
Return True
End Get
End Property
' Do not allow deletions
Public Overrides ReadOnly Property CanDelete() As Boolean
Get
Return False
End Get
End Property
' Do not allow insertions
Public Overrides ReadOnly Property CanInsert() As Boolean
Get
Return False
End Get
End Property
' Do not allow updates
Public Overrides ReadOnly Property CanUpdate() As Boolean
Get
Return False
End Get
End Property
' Do not allow paging
Public Overrides ReadOnly Property CanPage() As Boolean
Get
Return False
End Get
End Property
' Do not allow sorting
Public Overrides ReadOnly Property CanSort() As Boolean
Get
Return False
End Get
End Property
End Class
' A custom View Schema class
Public Class BookListViewSchema
Implements IDataSourceViewSchema
Public Sub New()
End Sub
' The name of this View Schema
Public ReadOnly Property Name() As String Implements IDataSourceViewSchema.Name
Get
Return "BookList"
End Get
End Property
' Build a Field Schema array
Public Function GetFields() As IDataSourceFieldSchema() Implements IDataSourceViewSchema.GetFields
Dim fields(1) As IDataSourceFieldSchema
fields(0) = New CustomIDFieldSchema()
fields(1) = New CustomTitleFieldSchema()
Return fields
End Function
' There are no child views, so return Nothing
Public Function GetChildren() As IDataSourceViewSchema() Implements IDataSourceViewSchema.GetChildren
Return Nothing
End Function
End Class
' A custom Field Schema class for ID
Public Class CustomIDFieldSchema
Implements IDataSourceFieldSchema
Public Sub New()
End Sub
' Name is ID
Public ReadOnly Property Name() As String Implements IDataSourceFieldSchema.Name
Get
Return "ID"
End Get
End Property
' Data type is string
Public ReadOnly Property DataType() As Type Implements IDataSourceFieldSchema.DataType
Get
Return GetType(String)
End Get
End Property
' This is not an Identity field
Public ReadOnly Property Identity() As Boolean Implements IDataSourceFieldSchema.Identity
Get
Return False
End Get
End Property
' This field is read only
Public ReadOnly Property IsReadOnly() As Boolean Implements IDataSourceFieldSchema.IsReadOnly
Get
Return True
End Get
End Property
' This field is unique
Public ReadOnly Property IsUnique() As Boolean Implements IDataSourceFieldSchema.IsUnique
Get
Return True
End Get
End Property
' This field can't be longer than 20
Public ReadOnly Property Length() As Integer Implements IDataSourceFieldSchema.Length
Get
Return 20
End Get
End Property
' This field can't be null
Public ReadOnly Property Nullable() As Boolean Implements IDataSourceFieldSchema.Nullable
Get
Return False
End Get
End Property
' This is a Primary Key
Public ReadOnly Property PrimaryKey() As Boolean Implements IDataSourceFieldSchema.PrimaryKey
Get
Return True
End Get
End Property
' These properties do not apply
Public ReadOnly Property Precision() As Integer Implements IDataSourceFieldSchema.Precision
Get
Return -1
End Get
End Property
Public ReadOnly Property Scale() As Integer Implements IDataSourceFieldSchema.Scale
Get
Return -1
End Get
End Property
End Class
' A custom Field Schema class for Title
Public Class CustomTitleFieldSchema
Implements IDataSourceFieldSchema
Public Sub New()
End Sub
' Name is Title
Public ReadOnly Property Name() As String Implements IDataSourceFieldSchema.Name
Get
Return "Title"
End Get
End Property
' Type is string
Public ReadOnly Property DataType() As Type Implements IDataSourceFieldSchema.DataType
Get
Return GetType(String)
End Get
End Property
' This is not an Identity field
Public ReadOnly Property Identity() As Boolean Implements IDataSourceFieldSchema.Identity
Get
Return False
End Get
End Property
' This field is not read only
Public ReadOnly Property IsReadOnly() As Boolean Implements IDataSourceFieldSchema.IsReadOnly
Get
Return False
End Get
End Property
' This field is not unique
Public ReadOnly Property IsUnique() As Boolean Implements IDataSourceFieldSchema.IsUnique
Get
Return False
End Get
End Property
' This field can't be longer than 100
Public ReadOnly Property Length() As Integer Implements IDataSourceFieldSchema.Length
Get
Return 100
End Get
End Property
' This field can't be null
Public ReadOnly Property Nullable() As Boolean Implements IDataSourceFieldSchema.Nullable
Get
Return False
End Get
End Property
' This is not the Primary Key
Public ReadOnly Property PrimaryKey() As Boolean Implements IDataSourceFieldSchema.PrimaryKey
Get
Return False
End Get
End Property
' These properties do not apply
Public ReadOnly Property Precision() As Integer Implements IDataSourceFieldSchema.Precision
Get
Return -1
End Get
End Property
Public ReadOnly Property Scale() As Integer Implements IDataSourceFieldSchema.Scale
Get
Return -1
End Get
End Property
End Class
End Namespace
...
<%@ Page Language="VB" %>
<%@ Register TagPrefix="aspSample"
Namespace="ASPNet.Design.Samples_VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<aspSample:CustomDataSource runat="server"
ID="CustomDS1"></aspSample:CustomDataSource>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Security.Permissions;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;
using System.Web.UI.Design.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;
namespace ASPNet.Design.Samples_CS
{
[Designer(typeof(CustomDataSourceDesigner)),
ToolboxData("<{0}:CustomDataSource runat=\"server\"></{0}:CustomDataSource>")]
public class CustomDataSource : ObjectDataSource
{
private ObjectDataSourceView _view = null;
private string _defaultViewName = "BookList";
public CustomDataSource() : base() { }
// Gets a view by name
protected override DataSourceView GetView(string viewName)
{
// This data source only allows one view
if (viewName != _defaultViewName)
return null;
else if (_view == null)
{
_view = new CustomDataSourceView(this,
_defaultViewName, HttpContext.Current);
}
return _view;
}
// Gets a list of view names for this class
protected override ICollection GetViewNames()
{
ArrayList ar = new ArrayList(1);
ar.Add(_defaultViewName);
return ar as ICollection;
}
}
// The runtime data source view
public class CustomDataSourceView : ObjectDataSourceView
{
private ArrayList _data = null;
public CustomDataSourceView(CustomDataSource owner,
string viewName, HttpContext context)
: base(owner, viewName, context)
{
owner.SelectCountMethod = "GetCount";
}
// This method would typically get a set of live data
// rather than create some dummy data
protected override IEnumerable ExecuteSelect(
DataSourceSelectArguments arguments)
{
if (_data == null)
{
// Create a set of runtime fake data
_data = new ArrayList();
_data.Add(new BookItem("ID_1", "Runtime Title 01"));
_data.Add(new BookItem("ID_2", "Runtime Title 02"));
_data.Add(new BookItem("ID_3", "Runtime Title 03"));
}
return _data as IEnumerable;
}
// Allow getting the record count
public override bool CanRetrieveTotalRowCount
{
get { return true; }
}
// Returns the number of records in the current set of data
public int GetCount()
{
if (_data == null)
return 0;
else
return _data.Count;
}
// Do not allow deletions
public override bool CanDelete
{
get { return false; }
}
// Do not allow insertions
public override bool CanInsert
{
get { return false; }
}
// Do not allow paging
public override bool CanPage
{
get { return false; }
}
// Do not allow sorting
public override bool CanSort
{
get { return false; }
}
// Do not allow updating
public override bool CanUpdate
{
get { return false; }
}
}
// A class to define each record of the data
public class BookItem
{
private string _id;
private string _title;
public BookItem(string id, string title)
{
_id = id;
_title = title;
}
public string ID
{
get { return _id; }
}
public string Title
{
get { return _title; }
set { _title = value; }
}
}
// Custom designer for the CustomDataSource control.
public class CustomDataSourceDesigner : DataSourceDesigner
{
private CustomDataSource _control;
private string _defaultViewName = "BookList";
private CustomDesignDataSourceView _view = null;
// Initialize the designer
public override void Initialize(IComponent component)
{
base.Initialize(component);
_control = (CustomDataSource)Component;
}
// Get a view
public override DesignerDataSourceView GetView(string viewName)
{
if (!viewName.Equals(_defaultViewName))
return null;
if (_view == null)
{
_view = new CustomDesignDataSourceView(this,
_defaultViewName);
}
return _view;
}
// Get a list of view names
public override string[] GetViewNames()
{
return new string[] { "BookList" };
}
// Do not allow refreshing the schema
public override bool CanRefreshSchema
{
get { return false; }
}
// Do not allow resizing
public override bool AllowResize
{
get { return false; }
}
}
// A design-time data source view
public class CustomDesignDataSourceView : DesignerDataSourceView
{
private ArrayList _data = null;
public CustomDesignDataSourceView(
CustomDataSourceDesigner owner, string viewName)
: base(owner, viewName)
{}
// Get data for design-time display
public override IEnumerable GetDesignTimeData(
int minimumRows, out bool isSampleData)
{
if (_data == null)
{
// Create a set of design-time fake data
_data = new ArrayList();
for (int i = 1; i <= minimumRows; i++)
{
_data.Add(new BookItem("ID_" + i.ToString(),
"Design-Time Title 0" + i.ToString()));
}
}
isSampleData = true;
return _data as IEnumerable;
}
public override IDataSourceViewSchema Schema
{
get { return new BookListViewSchema(); }
}
// Allow getting the record count
public override bool CanRetrieveTotalRowCount
{
get { return true; }
}
// Do not allow deletions
public override bool CanDelete
{
get { return false; }
}
// Do not allow insertions
public override bool CanInsert
{
get { return false; }
}
// Do not allow updates
public override bool CanUpdate
{
get { return false; }
}
// Do not allow paging
public override bool CanPage
{
get { return false; }
}
// Do not allow sorting
public override bool CanSort
{
get { return false; }
}
}
// A custom View Schema class
public class BookListViewSchema : IDataSourceViewSchema
{
public BookListViewSchema()
{ }
// The name of this View Schema
public string Name
{
get { return "BookList"; }
}
// Build a Field Schema array
public IDataSourceFieldSchema[] GetFields()
{
IDataSourceFieldSchema[] fields = new IDataSourceFieldSchema[2];
fields[0] = new CustomIDFieldSchema();
fields[1] = new CustomTitleFieldSchema();
return fields;
}
// There are no child views, so return null
public IDataSourceViewSchema[] GetChildren()
{
return null;
}
}
// A custom Field Schema class for ID
public class CustomIDFieldSchema : IDataSourceFieldSchema
{
public CustomIDFieldSchema()
{ }
// Name is ID
public string Name
{
get { return "ID"; }
}
// Data type is string
public Type DataType
{
get { return typeof(string); }
}
// This is not an Identity field
public bool Identity
{
get { return false; }
}
// This field is read only
public bool IsReadOnly
{
get { return true; }
}
// This field is unique
public bool IsUnique
{
get { return true; }
}
// This field can't be longer than 20
public int Length
{
get { return 20; }
}
// This field can't be null
public bool Nullable
{
get { return false; }
}
// This is a Primary Key
public bool PrimaryKey
{
get { return true; }
}
// These properties do not apply
public int Precision
{
get { return -1; }
}
public int Scale
{
get { return -1; }
}
}
// A custom Field Schema class for Title
public class CustomTitleFieldSchema : IDataSourceFieldSchema
{
public CustomTitleFieldSchema()
{ }
// Name is Title
public string Name
{
get { return "Title"; }
}
// Type is string
public Type DataType
{
get { return typeof(string); }
}
// This is not an Identity field
public bool Identity
{
get { return false; }
}
// This field is not read only
public bool IsReadOnly
{
get { return false; }
}
// This field is not unique
public bool IsUnique
{
get { return false; }
}
// This field can't be longer than 100
public int Length
{
get { return 100; }
}
// This field can't be null
public bool Nullable
{
get { return false; }
}
// This is not the Primary Key
public bool PrimaryKey
{
get { return false; }
}
// These properties do not apply
public int Precision
{
get { return -1; }
}
public int Scale
{
get { return -1; }
}
}
}
...
<%@ Page Language="C#" %>
<%@ Register TagPrefix="aspSample" Namespace="ASPNet.Design.Samples_CS" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<aspSample:CustomDataSource ID="CustomDS1"
runat="server"></aspSample:CustomDataSource>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>