p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 2.0 Professional (http://p2p.wrox.com/forumdisplay.php?f=215)
-   -   problem with viewstate and postback (http://p2p.wrox.com/showthread.php?t=68362)

hertendreef May 25th, 2008 04:26 PM

problem with viewstate and postback

i experimented with postback and viewstate. With this code, there are 2
dropdownlists created, one visible and with AutoPostBack true, the other not
visible and no AutoPostBack, and one button . The first time, when i choose
value "b" of DD1, the second DD appears. That's normal.

Now, what i don't understand is when i further click on the button (causing
a postback), the second DD remains visible. Don't think i don't want it. I
just don't uderstand.

After clicking the button, I thought the code would start from the very
begin, so setting DD2 again on not visible. And because the first DD1 has
not changed its selectedvalue, the procedure dropd changing DD2 into visible
will not be executed. So DD2 should stay invisible. But it's not.

Can somebody explain what's wrong in my way of thinking?
By the way, i tried with EnableViewState="false" in the page and ... same result: DD2 is still visible!


Imports System.Collections.Generic
Partial Class _Default
    Inherits System.Web.UI.Page
    Friend dds As New List(Of DropDownList)

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreInit
        Dim dd1, dd2 As DropDownList
        Dim z1, z2 As ListItem
        Dim lit As LiteralControl
        If Not IsPostBack Then
            dd1 = New DropDownList
            dd1.ID = "dd1"
            dd1.AutoPostBack = True
            z1 = New ListItem("a", "a")
            z1 = New ListItem("b", "b")

            dd2 = New DropDownList
            dd2.ID = "dd2"
            dd2.Visible = "false"
            z2 = New ListItem("a", "a")
            z2 = New ListItem("b", "b")

            Session("dds") = dds
            dds = CType(Session("dds"), List(Of DropDownList))
            For Each d As DropDownList In dds
        End If

        Dim bt2 As New Button
        AddHandler bt2.Click, AddressOf submit_Click

        For Each d As DropDownList In dds
            AddHandler d.SelectedIndexChanged, AddressOf dropd
    End Sub

    Protected Sub dropd(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim dd As DropDownList = CType(sender, DropDownList)
        Session("sv" & dd.ID) = dd.SelectedValue

        If dd.ID = "dd1" And dd.SelectedValue = "b" Then
            FindControl("dd2").Visible = True
        ElseIf dd.ID = "dd1" And Not dd.SelectedValue = "b" Then
            FindControl("dd2").Visible = False
        End If
    End Sub

    Protected Sub submit_Click(ByVal sender As Object, ByVal e As

    End Sub
End Class

hertendreef May 31st, 2008 04:05 PM

I found it : Session("dds") = dds retains the values of the ddl.

All times are GMT -4. The time now is 10:12 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.