Bonjour, Zooffy m'a aidé il y a quelques jours pour le script dont vous trouverez l'intégralité du code ci-dessous.
En fait il s'agit de créer en codebehind des templatefields.

J'ai juste un seul problème: je bute constamment pour la ligne tout en abs en rouge sur le message d'erreur suivant:
Détails de l'exception: System.NullReferenceException: La référence d'objet n'est pas définie à une instance d'un objet.


Comme Zooffy est en vacances jusqu'en septembre et si quelqu'un a la solution, cela m'arrangerait bien.

Merci d'avance.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
Imports Wib_screenMessages
Partial Class DisplayPictures_Zooffy
    Inherits System.Web.UI.Page
    Public pLang As String

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Dim bf As BoundField = Nothing
        Dim hf As HyperLinkField = Nothing
        Dim apercu As New TemplateField
        With apercu
            .ItemTemplate = New TemplatePopup(DataControlRowType.DataRow, "Id", "Apercu")
            .HeaderTemplate = New TemplatePopup(DataControlRowType.Header, "Apercu", "Apercu")
        End With

        pLang = Request.QueryString("Lang")

        If Not Page.IsPostBack Then

            bf = New BoundField
            bf.HeaderText = getWibMsg(796, "Sort", pLang)
            bf.DataField = "idSupplier"
            bf.SortExpression = "idSupplier"
            Logos.Columns.Add(bf)
            bf.ItemStyle.Width = New Unit("140", UnitType.Pixel)

            bf = New BoundField
            bf.HeaderText = getWibMsg(797, "Sort", pLang)
            bf.DataField = "id"
            bf.SortExpression = "id"
            Logos.Columns.Add(bf)
            bf.ItemStyle.Width = New Unit("140", UnitType.Pixel)

            apercu = New TemplateField
            apercu.HeaderText = getWibMsg(798, "Sort", pLang)
            Logos.Columns.Add(apercu)

        End If

    End Sub
    Public Class TemplatePopup
        Implements ITemplate

        Private templateType As DataControlRowType
        Private columnName As String 'le nom de la colonne de la source de données
        Private controleName As String 'le nom du contrôle à mettre dans la colonne

        Sub New(ByVal type As DataControlRowType, ByVal colname As String, ByVal ctrlName As String)
            templateType = type
            columnName = colname
            controleName = ctrlName
        End Sub

        Sub InstantiateIn(ByVal container As System.Web.UI.Control) _
         Implements ITemplate.InstantiateIn

            Select Case templateType
                Case DataControlRowType.Header
                    Dim lc As New Literal
                    lc.Text = "<b>" & columnName & "</b>"
                    container.Controls.Add(lc)

                Case DataControlRowType.DataRow
                    Select Case controleName
                        Case "Apercu"
                            Dim hl As New HyperLink
                            AddHandler hl.DataBinding, AddressOf hl_DataBinding
                            container.Controls.Add(hl)
                        Case "Actif"
                            Dim ib As New LinkButton
                            AddHandler ib.DataBinding, AddressOf ib_DataBinding
                            container.Controls.Add(ib)
                        Case Else

                    End Select

                Case Else
                    ' Insert code to handle unexpected values. 
            End Select

        End Sub

        Private Sub hl_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
            Dim l As HyperLink = CType(sender, HyperLink)
            Dim row As GridViewRow = CType(l.NamingContainer, GridViewRow)
            l.Text = DataBinder.Eval(row.DataItem, "Id").ToString()
            l.ID = "hlCamp" & DataBinder.Eval(row.DataItem, "Id").ToString()

        End Sub

        Private Sub ib_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
            Dim l As LinkButton = CType(sender, LinkButton)
            Dim row As GridViewRow = CType(l.NamingContainer, GridViewRow)
            l.ID = "ibActif" & DataBinder.Eval(row.DataItem, "Id").ToString()
            l.Enabled = True
            l.CommandName = "chgActif"
        End Sub

    End Class
    Protected Sub Logos_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Logos.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If e.Row.RowType = DataControlRowType.DataRow Then

                Dim lnk As HyperLink = CType(e.Row.FindControl("hlCamp" & CType(DataBinder.Eval(e.Row, "DataItem.Id"), String)), HyperLink)
                With lnk
                    .NavigateUrl = "~/DisplayPictures/ShowPicture2.aspx?id=2"
                    .ImageUrl = "~/DisplayPictures/Mvu.jpg"
                End With
            End If
        End If
    End Sub


End Class
Ci-dessous la pile:
[NullReferenceException: La référence d'objet n'est pas définie à une instance d'un objet.]
DisplayPictures_Zooffy.Logos_RowDataBound(Object sender, GridViewRowEventArgs e) in C:\Documents and Settings\Mvu\Mes documents\Visual Studio 2008\WebSites\Wib2008v2\DisplayPictures\Zooffy.aspx.vb:105
System.Web.UI.WebControls.GridView.OnRowDataBound(GridViewRowEventArgs e) +105
System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +233
System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +2957
System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +59
System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +11
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +111
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +29
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +149
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +70
System.Web.UI.WebControls.GridView.DataBind() +4
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +69
System.Web.UI.Control.EnsureChildControls() +87
System.Web.UI.Control.PreRenderRecursiveInternal() +50
System.Web.UI.Control.PreRenderRecursiveInternal() +170
System.Web.UI.Control.PreRenderRecursiveInternal() +170
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2041