D
Dan
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?
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
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?
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