Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2007

Tip: Looking for answers? Try searching our database.

selectedvalue postback problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bernard Borsu - 04 Jul 2007 18:41 GMT
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 !

Signature

Bernard
bernard.borsu@odysseos.net

Milosz Skalecki [MCAD] - 04 Jul 2007 19:12 GMT
Paste some code
Signature

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.
[quoted text clipped - 13 lines]
>
> Thanks, in advance !
Bernard Borsu - 05 Jul 2007 00:33 GMT
Portion of code in the aspx page :
...
<tr>

<td class="C1">

<asp:Literal ID="oLLieuRepos" EnableViewState="False" Runat="server" />

<br>

<asp:DropDownList ID="oLieuRepos" Width="90%" Runat="server" />

</td>

<td class="C2">

<asp:Literal ID="oLHLieuRepos" EnableViewState="False" Runat="server" />

</td>

</tr>

....

Portion of code in the codebehind file :

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

If Page.IsPostBack Then

Dim nI As Integer

For nI = 1 To oNCS.Value

NouveauBlocConjoint(nI)

Next

Else

oLieuRepos.DataTextField = "Fune_NomLocalise"

oLieuRepos.DataValueField = "Fune_Id"

oPage.BD.Charge(IIf(lPFGP, "Proc_OPFunerariumsEPF", "Proc_FunerariumsEPF"),
oLieuRepos, New SqlParameter("@EPF", Utilisateur()))

End If

End Sub

Charge() is my own function executing SQL request dans binding data in the
DDL.

> Paste some code
>
[quoted text clipped - 18 lines]
>>
>> Thanks, in advance !
Bernard Borsu - 05 Jul 2007 09:35 GMT
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.

> Paste some code
>
[quoted text clipped - 18 lines]
>>
>> Thanks, in advance !
Milosz Skalecki [MCAD] - 05 Jul 2007 11:30 GMT
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
Signature

Milosz

> Another information :
>
[quoted text clipped - 25 lines]
> >>
> >> Thanks, in advance !
Bernard Borsu - 05 Jul 2007 12:30 GMT
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 ?

> Bernard,
>
[quoted text clipped - 172 lines]
>> >>
>> >> Thanks, in advance !
Milosz Skalecki [MCAD] - 05 Jul 2007 14:10 GMT
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
Signature

Milosz

> Thanks for this explanation. I'm totally agree with it. But it's not really
> the good context.
[quoted text clipped - 194 lines]
> >> >>
> >> >> Thanks, in advance !
Bernard Borsu - 05 Jul 2007 15:04 GMT
Hi !

In this case, this is not the real reason of the problem. I've tried to test
the ddl in a simple page with the minimum of elements. In this case,
everything works fine :

Aspx page :

<%@ Page Language="vb" AutoEventWireup="false"
MasterPageFile="/Cadres/Enaos.Master" CodeBehind="SaleteBis.aspx.vb"
Inherits="PFGP_Partenaires.SaleteBis" %>

<asp:Content ID="oC" ContentPlaceHolderID="oH" runat="server">

<asp:DropDownList ID="oLieuRepos" runat="server" />

<asp:Button ID="oOK" runat="server" />

</asp:Content>

Codebehind file :

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

If IsPostBack Then

Response.Write(oLieuRepos.SelectedIndex & "/" & oLieuRepos.SelectedValue &
"/" & oLieuRepos.EnableViewState & "<br>")

Else

BD.Charge("Proc_OPFunerariumsEPF", oLieuRepos, New SqlParameter("@EPF",
Utilisateur()))

With oLieuRepos

With .Items()

.Add(New ListItem(Ressource("OptReposDomicile"), -1))

.Add(New ListItem(Ressource("OptReposAutreFunerarium"), -2))

.Add(New ListItem(Ressource("OptReposAutre"), -3))

End With

End With

End If

End Sub

In fact, the bad result is due to 2 form tag in the page. One is the aspx
form tag and the second one is for searching an item in the database. If i
remove this search form tag, in the postback, the selectedvalue property
return the good value and the ddl items are stil present.

Many thanks for your attention.

> Hi again,
>
[quoted text clipped - 224 lines]
>> >> >>
>> >> >> Thanks, in advance !
Bernard Borsu - 05 Jul 2007 15:57 GMT
Last detail : problem due to 2 form tags, but one without method "post"
explicitly declared.

So, now everything functinable with on pure html form with method post
declared and on aspx form

> Hi again,
>
[quoted text clipped - 224 lines]
>> >> >>
>> >> >> Thanks, in advance !

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.