1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
Imports System.Data
Public Class GrilleTemplate
Implements System.Web.UI.ITemplate
Dim _templateType As ListItemType
Dim _columnName As String
Dim _columnName2 As String
Dim _TypeControl As String = "TextBox"
Dim _Dts As DataSet
'constructeur pour les controles simples (Label,Textbox)
Public Sub New(ByVal Type As ListItemType, ByVal colname As String, ByVal TypeControl As String)
_templateType = Type
_columnName = colname
_TypeControl = TypeControl
End Sub
'constructeur pour les liaisons complexes (DropDownList)
Public Sub New(ByVal Type As ListItemType, ByVal colname As String, ByVal TypeControl As String, ByVal dts As DataSet)
_templateType = Type
_columnName = colname
_TypeControl = TypeControl
_Dts = dts
End Sub
Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn
Try
Select Case _templateType
'définition des controles pour le header
Case ListItemType.Header
Select Case UCase(_TypeControl)
'en tete des colonnes avec liens pour trier + textbox de recherche
Case "TEXTBOX"
Dim lb As LinkButton = New LinkButton
lb.Text = Me._columnName + "<br>"
lb.CommandName = "Sort"
lb.ID = "lblEnTete" + Me._columnName
lb.CommandArgument = Me._columnName
container.Controls.Add(lb)
Dim tb As TextBox = New TextBox
tb.Width = 100
tb.ID = "search_" + Me._columnName
container.Controls.Add(tb)
'en tete de la colonne des fonctions contenant le bouton de recherche
Case "BUTTON"
Dim lb As Label = New Label
lb.Text = "<br>"
container.Controls.Add(lb)
Dim tb As LinkButton = New LinkButton
tb.Text = "Rechercher"
tb.ID = "Search"
tb.CommandName = "Search"
container.Controls.Add(tb)
End Select
' définition des controles pour les lignes
Case ListItemType.Item
Select Case UCase(_TypeControl)
' définition des boutons pour la colonne de fonction (Edit, select, delete)
Case "BUTTON"
Dim tb1 As LinkButton = New LinkButton()
tb1.Text = "Editer"
tb1.ID = "btn_Edit"
tb1.CommandName = "Edit"
container.Controls.Add(tb1)
'affichage des items en format label
Case "LABEL"
Dim tb1 As Label = New Label()
tb1.BorderStyle = BorderStyle.None
AddHandler tb1.DataBinding, AddressOf tb1_DataBindingLabel
container.Controls.Add(tb1)
'affichage des listbox
Case "LISTBOX"
Dim mylistbox As DropDownList = New DropDownList
mylistbox.DataSource = _Dts.Tables(0)
AddHandler mylistbox.DataBinding, AddressOf tb1_DataBindingListBox
mylistbox.DataTextField = _columnName
mylistbox.DataValueField = _columnName2
mylistbox.ID = ("lstDynamique")
mylistbox.Enabled = False
container.Controls.Add(mylistbox)
End Select
'définition des controles pour l'édition de la ligne
Case ListItemType.EditItem
Select Case UCase(_TypeControl)
'boutons de la colonne des options (update, cancel)
Case "BUTTON"
Dim tb1 As LinkButton = New LinkButton()
tb1.CommandName = "Update"
tb1.Text = "Enregistrer"
container.Controls.Add(tb1)
Dim lbl1 As Label = New Label()
lbl1.Text = " | "
container.Controls.Add(lbl1)
Dim tb2 As LinkButton = New LinkButton()
tb2.CommandName = "Cancel"
tb2.ID = "btn_CancelEdit"
tb2.Text = "Annuler"
container.Controls.Add(tb2)
'maitient du format label pour les champs non modifiables
Case "LABEL"
Dim tb1 As Label = New Label()
tb1.BorderStyle = BorderStyle.None
AddHandler tb1.DataBinding, AddressOf tb1_DataBindingLabel
container.Controls.Add(tb1)
'transformation des labels en textbox pour la modification
Case "TEXTBOX"
Dim tb1 As TextBox = New TextBox()
AddHandler tb1.DataBinding, AddressOf tb1_DataBindingTextBox
container.Controls.Add(tb1)
'affichage de la listbox en format modifiable
Case "LISTBOX"
Dim mylistbox As DropDownList = New DropDownList
mylistbox.DataSource = _Dts.Tables(0)
AddHandler mylistbox.DataBinding, AddressOf tb1_DataBindingListBox
mylistbox.DataTextField = _columnName
mylistbox.DataValueField = _columnName2
mylistbox.ID = ("lstDynamique")
mylistbox.Enabled = True
container.Controls.Add(mylistbox)
End Select
End Select
Catch ex As Exception
End Try
End Sub
#Region "Binding des données "
Sub tb1_DataBindingLabel(ByVal sender As Object, ByVal e As EventArgs)
Dim lbldata As Label = CType(sender, Label)
Dim container As GridViewRow = CType(lbldata.NamingContainer, GridViewRow)
Dim dataValue As Object = DataBinder.Eval(container.DataItem, _columnName)
If Not IsNothing(dataValue) Then
lbldata.Text = dataValue.ToString
End If
End Sub
Sub tb1_DataBindingTextBox(ByVal sender As Object, ByVal e As EventArgs)
Dim lbldata As TextBox = CType(sender, TextBox)
Dim container As GridViewRow = CType(lbldata.NamingContainer, GridViewRow)
Dim dataValue As Object = DataBinder.Eval(container.DataItem, _columnName)
If Not IsNothing(dataValue) Then
lbldata.Text = dataValue.ToString
End If
End Sub
Sub tb1_DataBindingListBox(ByVal sender As Object, ByVal e As EventArgs)
Dim lst As DropDownList = CType(sender, DropDownList)
Dim container As GridViewRow = CType(lst.NamingContainer, GridViewRow)
Dim dataValue As Object = DataBinder.Eval(container.DataItem, _columnName)
If Not IsNothing(dataValue) Then
lst.SelectedValue = dataValue.ToString
End If
End Sub
#End Region
End Class |
Partager