Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 25th, 2008, 04:26 PM
Friend of Wrox
 
Join Date: Apr 2006
Location: , , .
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem with viewstate and postback

Hi,

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!

Thanks
Dan


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")
            dd1.Items.Add(z1)
            z1 = New ListItem("b", "b")
            dd1.Items.Add(z1)

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

            dds.Add(dd1)
            dds.Add(dd2)
            form1.Controls.Add(dd1)
            form1.Controls.Add(dd2)
            Session("dds") = dds
        Else
            dds = CType(Session("dds"), List(Of DropDownList))
            For Each d As DropDownList In dds
                form1.Controls.Add(d)
            Next
        End If

        Dim bt2 As New Button
        form1.Controls.Add(bt2)
        AddHandler bt2.Click, AddressOf submit_Click

        For Each d As DropDownList In dds
            AddHandler d.SelectedIndexChanged, AddressOf dropd
        Next
    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
System.EventArgs)

    End Sub
End Class



  #2 (permalink)  
Old May 31st, 2008, 04:05 PM
Friend of Wrox
 
Join Date: Apr 2006
Location: , , .
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
textbox viewstate missing on postback JoshuaZ Classic ASP Basics 6 August 27th, 2008 03:45 PM
Problem with DropDownList's Viewstate. vinod ASP.NET 2.0 Basics 1 January 19th, 2008 08:07 AM
problem with viewstate? dll? namespace? vmw@email.byu.edu ASP.NET 1.0 and 1.1 Basics 0 June 19th, 2003 02:52 PM





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