Re: selectedvalue postback problem

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi again,

That's exactly what i was trying to explain. You have to follow the way i
presented in my previous post. Items are restored from the viewstate, but you
have to recreate the dropdown list first, and they it'll take care of
repopulating items collection itself. The only requirement is the control
tree must be the same (meaning, the structure of the datalist has to be
exactly as it was before viewstate was serialized). Try to amend your code
using my example and you'll see dropdownlist will contain the same items as
before postback. If you have any more questions, i'll be pleased to answer.

regards
--
Milosz


"Bernard Borsu" wrote:

Thanks for this explanation. I'm totally agree with it. But it's not really
the good context.

I think my details were badly explained.

I'm not building my dropdownlist dynamically. I fill my dropdownlist
dynamically.

When listitems of my dropdownlist are define in the aspx page, i've no
problem to use the selectedvalue property when the postback event occurs.

At the opposite, When listitems of my dropdownlist are created in the
codebehind file, in the page_load (conditioned by not ispostback()), i can't
use the selectedvalue property when the postback event occurs. All the items
of the ddl are not presents. It seems that loadviewstate did not operate for
this control. It was the case in asp.net 1.1. I use exactly the same code
i've used in asp.net 1.1 and the result is not the same in asp.net 2.0.

Sorry but my english is not very fine.

Is it more clear ?

"Milosz Skalecki [MCAD]" <mily242@xxxxxxxxxxxxxxxxx> a écrit dans le message
de news: 18D0B8F1-4F9B-45BC-84A8-CA1190864331@xxxxxxxxxxxxxxxx
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:DataList 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


"Bernard Borsu" wrote:

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]" <mily242@xxxxxxxxxxxxxxxxx> a écrit dans le
message
de news: 5FBE6ADB-C5F0-4B38-B5EF-DBA325E03146@xxxxxxxxxxxxxxxx
Paste some code
--
Milosz


"Bernard Borsu" wrote:

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 !

--
Bernard
bernard.borsu@xxxxxxxxxxxx









.


Quantcast