Bernard,
Every time you build controls dynamically, you are responsible for
recreating them on postback. I suspect you way of thinking is once
control
has been instantiated dynamically the entire control is kept in the
viewstate. I am right? Well, this is not true. Controls store only some
of
the information required for rebuilding. If control has not been
instantiated
on postback, it's obvious if it does not exist it cannot restore its
state
from viewstate. Hence, if you want to build DataList dynamically,
handle
itemcreated event to instantiate the controls for every row. I prepared
a
fully working example to show you how it should be done.
-- aspx page code --
<asp
ataList ID="dataList" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
-- code beside / behind --
Imports System.Data
Imports System.Collections.Generic
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub dataList_ItemDataBound( _
ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) _
Handles dataList.ItemDataBound
With e.Item
If .ItemType = ListItemType.Item Or _
.ItemType = ListItemType.AlternatingItem Then
Dim row As DataRow = CType(.DataItem, DataRowView).Row
Dim label As Label = .FindControl(LabelId)
label.Text = row("Name")
Dim textBox As TextBox = .FindControl(TextBoxId)
textBox.Text = row("Date")
End If
End With
End Sub
Protected Sub dataList_ItemCreated(_
ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) _
Handles dataList.ItemCreated
With e.Item
If .ItemType = ListItemType.Item Or _
.ItemType = ListItemType.AlternatingItem Then
' label
Dim label As New Label()
label.ID = LabelId
label.ForeColor = Drawing.Color.Blue
.Controls.Add(label)
' text box
Dim textBox As New TextBox
textBox.ID = TextBoxId
textBox.Width = New Unit(200)
.Controls.Add(textBox)
End If
End With
End Sub
Protected Sub Page_Load( _
ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
dataList.DataSource = GetData()
dataList.DataBind()
End If
End Sub
Private Const TextBoxId As String = "txt"
Private Const LabelId As String = "lbl"
Private Function GetData() As DataTable
Dim table As New DataTable()
Dim row As DataRow
table.Columns.Add("Id", GetType(Integer))
table.Columns.Add("Name", GetType(String))
table.Columns.Add("Date", GetType(DateTime))
For i As Integer = 1 To 10
row = table.NewRow()
row(0) = i
row(1) = "Name" + i.ToString()
row(2) = DateTime.Now.AddMinutes(i)
table.Rows.Add(row)
Next
Return table
End Function
Protected Sub btnSubmit_Click(_
ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnSubmit.Click
Dim dates As New List(Of DateTime)
For Each item As DataListItem In dataList.Items
Dim textBox As TextBox = item.FindControl(TextBoxId)
dates.Add(DateTime.Parse(textBox.Text))
Next
End Sub
End Class
hope this helps
--
Milosz
:
Another information :
When the dropdownlist is statis, so defined in the aspx page, no
problem
on
the postback.
This problem occurs only when the dropdownlist is filled dynamically
by a
SQL request in the codebehind file.
"Milosz Skalecki [MCAD]" <
[email protected]> a écrit dans le
message
de news: (e-mail address removed)...
Paste some code
--
Milosz
:
I've a problem with selectedvalue from a dropdownlist. I've
migrated a
web
page from asp.net 1.1 to 2.0. Everything worked fine in asp.net
1.1.
When postback occurs, no values left in the dropdownlist :
selectedvalue
=
nothing, itemcollection is empty.
Enableviewstate is true for the ddl and all the parents of the ddl.
Databinding is conditioned with 'if not page.ispostback'
I really don't understand where is the problem. I made another web
migration
from 1.1 to 2.0 with another web page and in this case everything
works
fine.
If someone have an idea, i'm really really really interested
because
i've
searched the solution for this day long. :-((
Thanks, in advance !