Dynamic DataList HELP needed !!!!

S

Søren M. Olesen

I'm trying to create an .aspx page with a DataList where the ChildControls are created using the ITemplate. I want to be able to change the text without first having to select 'Edit' or something first and I don't want a postback each time I change something but only when I press a submit button. I've tried using the following code, but on postback, I'm unable to get the values of the childcontrols contained inside the DataList....What's the correct way to do this ??

Public Class WebForm1
Inherits System.Web.UI.Page

#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 Button1 As System.Web.UI.WebControls.Button
Protected WithEvents DataList1 As System.Web.UI.WebControls.DataList
'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

Public Class myDLTemplate
Implements ITemplate
Private templateType As ListItemType
Private attribute As String
Private id As String
Private selectedvalue


Sub New(ByVal type As ListItemType, ByVal a As String, ByVal i As String)
templateType = type
attribute = a
id = i
End Sub

Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
Select Case templateType
Case ListItemType.Header
Dim l As Literal = New Literal
l.Text = attribute
container.Controls.Add(l)
Case ListItemType.Item
Dim tb As TextBox = New TextBox
tb.AutoPostBack = False
tb.EnableViewState = True
AddHandler tb.DataBinding, AddressOf Me.OnBindData
AddHandler tb.TextChanged, AddressOf Me.OnTextChanged
container.Controls.Add(tb)
End Select
End Sub


Private Sub OnTextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim text As String = "123"
End Sub


Sub OnBindData(ByVal sender As Object, ByVal e As System.EventArgs)
Dim tb As TextBox = CType(sender, TextBox)
Dim container As DataListItem
If Not IsNothing(tb.NamingContainer) Then
container = CType(tb.NamingContainer, DataListItem)
If Not IsNothing(container) Then
tb.Text = CStr(DataBinder.Eval(container.DataItem, attribute))
End If
End If
End Sub
End Class

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
Dim lov As DataTable = New DataTable
lov.Columns.Add(New DataColumn("id", GetType(String)))
lov.Columns.Add(New DataColumn("name", GetType(String)))
For i As Integer = 0 To 5
Dim dr As DataRow = lov.NewRow()
dr(0) = i
dr(1) = "Item" & i
lov.Rows.Add(dr)
Next
DataList1.Visible = True
DataList1.ID = "myDataList"
DataList1.EnableViewState = True
DataList1.HeaderTemplate = New myDLTemplate(ListItemType.Header, "LOV", Nothing)
DataList1.ItemTemplate = New myDLTemplate(ListItemType.Item, "name", "id")
DataList1.DataSource = lov
DataList1.DataBind()
End If
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Here I'd like to be able to get the current value of the TextBoxes indside the 'myDataList' control

End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top