Datagrid avec templates column creer dynamiquement

jusrob Messages postés 2 Date d'inscription mercredi 24 mai 2006 Statut Membre Dernière intervention 2 juin 2006 - 2 juin 2006 à 19:56
jusrob Messages postés 2 Date d'inscription mercredi 24 mai 2006 Statut Membre Dernière intervention 2 juin 2006 - 2 juin 2006 à 19:59
<form id="aspnetForm" name="aspnetForm" onsubmit="javascript:return WebForm_OnSubmit();" action="infomsg.aspx?ID=751725" method="post">

Bonjour,
quand je fait update(enregistrer), add(ajouter) ou delete(détruire) dans un datagrid avec des templatescolumn creer dynamiquement, findcontrol ne trouve pas mon mon id du textbox .... je crois que mon problème est à la création de mes templatecolumns mais je ne vois pas où est le problème.
Au secours !!! :)
Merci d'avance
Voici mon code vb au complet ....


</form>

1 réponse

jusrob Messages postés 2 Date d'inscription mercredi 24 mai 2006 Statut Membre Dernière intervention 2 juin 2006
2 juin 2006 à 19:59
Imports LibraryOracle
Imports LibraryMessage
Public Class GererParamTraitement
Inherits System.Web.UI.Page
Dim oracleLocal As OracleODP

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents LabTitre As System.Web.UI.WebControls.Label
Protected WithEvents DataGridTable As System.Web.UI.WebControls.DataGrid
Protected WithEvents DropListTable As System.Web.UI.WebControls.DropDownList
Protected WithEvents LabJavascript As System.Web.UI.WebControls.Label
Protected WithEvents LinkButton1 As System.Web.UI.WebControls.LinkButton
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Hidden1 As System.Web.UI.HtmlControls.HtmlInputHidden
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

oracleLocal = Session("oracle")

If Not Page.IsPostBack Then
'Charger la liste des noms de table avec web.config

End If

End Sub
Private Sub SelectionTable()
Dim Selection As Integer

Selection = Me.DropListTable.SelectedIndex

If Selection > 0 Then
AfficherTableau()
End If

End Sub

Private Sub AfficherTableau()

Dim NomTable As String

Dim TxtSql As String

NomTable = Me.DropListTable.SelectedValue

Dim DonneeTable As DataSet
DonneeTable = oracleLocal.LancerRequeteLecture("select * from " & ConfigurationSettings.AppSettings("schema") & NomTable, "DonneeTable")

Me.DataGridTable.DataSource = DonneeTable

For Each dtColumn As DataColumn In DonneeTable.Tables(0).Columns
Dim tc1 As TemplateColumn

Me.DataGridTable.Columns.Add(CreateTemplateColumn(dtColumn.ColumnName, dtColumn.ColumnName))
Next

Me.DataGridTable.DataBind()

End Sub
Class myLabelColumn
Implements ITemplate
Dim _Champ As String
Public Property Champ() As String
Get
Champ = _Champ
End Get
Set(ByVal Value As String)
_Champ = Value
End Set
End Property
Sub instantiatein(ByVal container As Control) Implements ITemplate.InstantiateIn
Dim lbl As Label = New Label
AddHandler lbl.DataBinding, AddressOf BindLabelColumn
container.Controls.Add(lbl)
End Sub
Public Sub BindLabelColumn(ByVal sender As Object, ByVal e As EventArgs)
Dim lbl As Label = CType(sender, Label)
Dim container As DataGridItem = CType(lbl.NamingContainer, DataGridItem)
Dim strVals As String

lbl.Text = Convert.ToString(DataBinder.Eval((CType(container, DataGridItem)).DataItem, _Champ))

lbl.ID = "lbl" & _Champ

End Sub
End Class
Class myTxtColumn
Implements ITemplate
Dim _Champ As String
Public Property Champ() As String
Get
Champ = _Champ
End Get
Set(ByVal Value As String)
_Champ = Value
End Set
End Property
Sub instantiatein(ByVal container As Control) Implements ITemplate.InstantiateIn
Dim txt As TextBox = New TextBox
Dim hiddentxt As HtmlControls.HtmlInputHidden = New HtmlControls.HtmlInputHidden
AddHandler txt.DataBinding, AddressOf BindTxtColumn
AddHandler hiddentxt.DataBinding, AddressOf BindHiddenColumn

container.Controls.Add(txt)
container.Controls.Add(hiddentxt)
End Sub
Public Sub BindTxtColumn(ByVal sender As Object, ByVal e As EventArgs)
Dim txt As TextBox = CType(sender, TextBox)
Dim container As DataGridItem = CType(txt.NamingContainer, DataGridItem)
Dim strVals As String

txt.Text = Convert.ToString(DataBinder.Eval((CType(container, DataGridItem)).DataItem, _Champ))
txt.ID = "txt" & _Champ

End Sub
Public Sub BindHiddenColumn(ByVal sender As Object, ByVal e As EventArgs)
Dim hiddentxt As HtmlControls.HtmlInputHidden = CType(sender, HtmlControls.HtmlInputHidden)
Dim container As DataGridItem = CType(hiddentxt.NamingContainer, DataGridItem)
Dim strVals As String

hiddentxt.Value = Convert.ToString(DataBinder.Eval((CType(container, DataGridItem)).DataItem, _Champ))

hiddentxt.ID = "hiddentxt" & _Champ

End Sub
End Class

Function CreateTemplateColumn(ByVal DataFieldValue As String, ByVal HeaderTextValue As String) As TemplateColumn

' Create a templateColumn.
Dim tplCol As New TemplateColumn

Dim monLabel As New myLabelColumn
Dim monTxt As New myTxtColumn

monLabel.Champ = DataFieldValue
monTxt.Champ = DataFieldValue

tplCol.ItemTemplate = monLabel

tplCol.EditItemTemplate = monTxt

tplCol.FooterTemplate = monTxt

tplCol.HeaderText = HeaderTextValue

Return tplCol

End Function

Private Sub DropListTable_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropListTable.SelectedIndexChanged
SelectionTable()
End Sub

Private Sub DataGridTable_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGridTable.EditCommand
Me.DataGridTable.EditItemIndex = e.Item.ItemIndex
AfficherTableau()
End Sub

Private Sub DataGridTable_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGridTable.UpdateCommand

Dim NomTable As String
Dim TxtSql As String
Dim reponse As String
Dim test As String
Dim TxtHiddenSql As String

'Prendre les valeurs sélectionné
NomTable = Me.DropListTable.SelectedValue

Dim DonneeTable As DataSet
DonneeTable = oracleLocal.LancerRequeteLecture("select * from " & ConfigurationSettings.AppSettings("schema") & NomTable, "DonneeTable")

'Construire le where de la commande sql
For Each dtColumn As DataColumn In DonneeTable.Tables(0).Columns
test = CType(DataGridTable.FindControl("hiddentxt" & dtColumn.ColumnName), HtmlControls.HtmlInputHidden).Value
TxtHiddenSql TxtHiddenSql & dtColumn.ColumnName & " " & "'" & test & "'" & " and "
Next

TxtHiddenSql = Left(TxtHiddenSql, Len(TxtHiddenSql) - 4)

'Construire la commande sql
For Each dtColumn As DataColumn In DonneeTable.Tables(0).Columns
TxtSql TxtSql & dtColumn.ColumnName & " " & "'" & CType(e.Item.FindControl("txt" & dtColumn.ColumnName), TextBox).Text & "'" & ", "
Next

TxtSql = Left(TxtSql, Len(TxtSql) - 2)

'Update l'enregistrement
If Not oracleLocal.LancerRequeteMAJ("Update " & ConfigurationSettings.AppSettings("schema") & NomTable & " set " & TxtSql & " WHERE " & TxtHiddenSql) Then
'Message d'erreur sur la suppresion de l'enregistrement
AfficherErreur(oracleLocal.GetDerniereErreur)
End If

Me.DataGridTable.EditItemIndex = -1
AfficherTableau()

End Sub
Private Sub DataGridTable_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGridTable.CancelCommand
Me.DataGridTable.EditItemIndex = -1
AfficherTableau()
End Sub
Private Sub DataGridTable_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGridTable.DeleteCommand

Dim NomTable As String
Dim TxtSql As String

'Prendre les valeurs sélectionné
NomTable = Me.DropListTable.SelectedValue

Dim DonneeTable As DataSet
DonneeTable = oracleLocal.LancerRequeteLecture("select * from " & ConfigurationSettings.AppSettings("schema") & NomTable, "DonneeTable")

'Construire la commande sql
For Each dtColumn As DataColumn In DonneeTable.Tables(0).Columns
TxtSql TxtSql & dtColumn.ColumnName & " " & "'" & CType(e.Item.FindControl("lbl" & dtColumn.ColumnName), Label).Text & "'" & " and "
Next

TxtSql = Left(TxtSql, Len(TxtSql) - 4)

'Delete de la table
If Not oracleLocal.LancerRequeteMAJ("DELETE FROM " & ConfigurationSettings.AppSettings("schema") & NomTable & " WHERE " & TxtSql) Then
'Message d'erreur sur la suppresion de l'enregistrement
AfficherErreur(oracleLocal.GetDerniereErreur)
End If

Me.DataGridTable.EditItemIndex = -1
AfficherTableau()

End Sub
Private Sub AfficherErreur(ByVal Message As String)
Response.Write(Message)
End Sub

Private Sub DataGridTable_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGridTable.ItemCommand
If e.CommandName = "Add" Then
Response.Write("Ajout")

Dim DonneeTable As DataSet
Dim NomTable As String
Dim TxtSql As String

'Prendre les valeurs sélectionné
NomTable = Me.DropListTable.SelectedValue

DonneeTable = oracleLocal.LancerRequeteLecture("select * from " & ConfigurationSettings.AppSettings("schema") & NomTable, "DonneeTable")

'Construire la commande sql
For Each dtColumn As DataColumn In DonneeTable.Tables(0).Columns
TxtSql = TxtSql & " ( " & dtColumn.ColumnName & ", "
Next

TxtSql = Left(TxtSql, Len(TxtSql) - 2)
TxtSql = TxtSql & ") "

For Each dtColumn As DataColumn In DonneeTable.Tables(0).Columns
TxtSql = TxtSql & "Values ( " & CType(e.Item.FindControl("txt" & dtColumn.ColumnName), TextBox).Text & ","
Next
TxtSql = Left(TxtSql, Len(TxtSql) - 2)
TxtSql = TxtSql & ") "

'Ajout dans la table
If Not oracleLocal.LancerRequeteMAJ("Insert into" & NomTable & TxtSql) Then
'Message d'erreur sur l'ajout de l'enregistrement
AfficherErreur(oracleLocal.GetDerniereErreur)
End If

Me.DataGridTable.EditItemIndex = e.Item.ItemIndex
AfficherTableau()

End If
End Sub


Private Sub DataGridTable_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGridTable.ItemCreated

Try
Dim btn As Button = CType(e.Item.Cells(1).Controls(0), Button)

Dim monmessage As New Message
monmessage.Type = "confirm"
monmessage.ContenuMessage = "Désirez-vous vraiment supprimer cette occurence ?"
monmessage.ActionTrue = ""
monmessage.ActionFalse = "return false;"

btn.Attributes.Add("onclick", monmessage.AfficherMessage())

Catch ex As Exception

End Try

End Sub
End Class
0
Rejoignez-nous