Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #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.

 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:25 AM.


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