C
Cas
Hi,
I want to use a detailsview only for inserting data into a database (for a
survey). In order to check the inputted data, i need Templatefield.
So I defined a detailsview and a SqlDataSource in the aspx file.
The creation of the templatefields are done programmatically, because the
number of fields vary (fieldnames are fetched from the same sqldatasource).
Now, my problem: no error, the detailsview renders the right fieldheaders
and the texboxs, but when clicking on the Inset button, i can see there is a
postback, but the ItemInserting procedure is not started (so no inserting)
and the texboxs are gone!!
Thanks for help
Cas
The code:
1) the class:
-----------
Public Class DetailsViewTemplate
Inherits System.Web.UI.Page
Implements ITemplate
Dim templatetype As ListItemType
Dim columnname As String
Public Sub New(ByVal type As ListItemType, ByVal vg As String)
templatetype = type
columnname = vg
End Sub
Private Sub InstantiateIn(ByVal container As Control) Implements
ITemplate.InstantiateIn
Case ListItemType.EditItem
Dim tb = New TextBox()
tb.Text = ""
container.Controls.Add(tb)
End Select
End Sub
End Class
2) code-behind
---------------
Imports System.Data.OleDb
Partial Class excel
Inherits System.Web.UI.Page
Friend nfield As Integer
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim sql As String
Dim oConnection As OleDbConnection
'Dim sConnectionString As String
oConnection = New OleDbConnection()
Dim comd As OleDbCommand
Dim dtreader As OleDbDataReader
SqlDataSource1.DataBind()
oConnection.ConnectionString = SqlDataSource1.ConnectionString
oConnection.Open()
Dim i As Integer
sql = "select count(*) from fld;"
comd = New OleDbCommand(sql, oConnection)
nfield = comd.ExecuteScalar
nfieldout = nfield
sql = "select * from fld;"
comd = New OleDbCommand(sql, oConnection)
dtreader = comd.ExecuteReader
Dim bf(nfield) As TemplateField
If Not Page.IsPostBack Then
For i = 0 To nfield - 1
dtreader.Read()
bf(i) = New TemplateField
bf(i).ItemTemplate = New
DetailsViewTemplate(ListItemType.Item, "fld" & nfield)
bf(i).InsertItemTemplate = New
DetailsViewTemplate(ListItemType.EditItem, "fld" & nfield)
DetailsView1.Fields.Add(bf(i))
Next
dtreader.Close()
Dim cf As CommandField
cf = New CommandField
cf.ShowInsertButton = True
DetailsView1.Fields.Add(cf)
End If
oConnection.Close()
End Sub
Protected Sub DetailsView1_ItemInserting(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles
DetailsView1.ItemInserting
Dim fd, vl, wd,inscomm, a, vlfin As String
Dim i, j, tel As Integer
For i = 1 To nfield
fd= fd & "field" & i & ","
vl = e.Values("fld" & i)
wd = wd & "'" & vlfin & "',"
vlfin = ""
Next
vragen = vragen & ") values ('" & lol & "',"
inscomm = "insert into data (login," & fd &wd
SqlDataSource1.InsertCommand = inscomm
SqlDataSource1.ProviderName = "System.Data.OleDb"
End Sub
End Class
I want to use a detailsview only for inserting data into a database (for a
survey). In order to check the inputted data, i need Templatefield.
So I defined a detailsview and a SqlDataSource in the aspx file.
The creation of the templatefields are done programmatically, because the
number of fields vary (fieldnames are fetched from the same sqldatasource).
Now, my problem: no error, the detailsview renders the right fieldheaders
and the texboxs, but when clicking on the Inset button, i can see there is a
postback, but the ItemInserting procedure is not started (so no inserting)
and the texboxs are gone!!
Thanks for help
Cas
The code:
1) the class:
-----------
Public Class DetailsViewTemplate
Inherits System.Web.UI.Page
Implements ITemplate
Dim templatetype As ListItemType
Dim columnname As String
Public Sub New(ByVal type As ListItemType, ByVal vg As String)
templatetype = type
columnname = vg
End Sub
Private Sub InstantiateIn(ByVal container As Control) Implements
ITemplate.InstantiateIn
Case ListItemType.EditItem
Dim tb = New TextBox()
tb.Text = ""
container.Controls.Add(tb)
End Select
End Sub
End Class
2) code-behind
---------------
Imports System.Data.OleDb
Partial Class excel
Inherits System.Web.UI.Page
Friend nfield As Integer
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim sql As String
Dim oConnection As OleDbConnection
'Dim sConnectionString As String
oConnection = New OleDbConnection()
Dim comd As OleDbCommand
Dim dtreader As OleDbDataReader
SqlDataSource1.DataBind()
oConnection.ConnectionString = SqlDataSource1.ConnectionString
oConnection.Open()
Dim i As Integer
sql = "select count(*) from fld;"
comd = New OleDbCommand(sql, oConnection)
nfield = comd.ExecuteScalar
nfieldout = nfield
sql = "select * from fld;"
comd = New OleDbCommand(sql, oConnection)
dtreader = comd.ExecuteReader
Dim bf(nfield) As TemplateField
If Not Page.IsPostBack Then
For i = 0 To nfield - 1
dtreader.Read()
bf(i) = New TemplateField
bf(i).ItemTemplate = New
DetailsViewTemplate(ListItemType.Item, "fld" & nfield)
bf(i).InsertItemTemplate = New
DetailsViewTemplate(ListItemType.EditItem, "fld" & nfield)
DetailsView1.Fields.Add(bf(i))
Next
dtreader.Close()
Dim cf As CommandField
cf = New CommandField
cf.ShowInsertButton = True
DetailsView1.Fields.Add(cf)
End If
oConnection.Close()
End Sub
Protected Sub DetailsView1_ItemInserting(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles
DetailsView1.ItemInserting
Dim fd, vl, wd,inscomm, a, vlfin As String
Dim i, j, tel As Integer
For i = 1 To nfield
fd= fd & "field" & i & ","
vl = e.Values("fld" & i)
wd = wd & "'" & vlfin & "',"
vlfin = ""
Next
vragen = vragen & ") values ('" & lol & "',"
inscomm = "insert into data (login," & fd &wd
SqlDataSource1.InsertCommand = inscomm
SqlDataSource1.ProviderName = "System.Data.OleDb"
End Sub
End Class