473,772 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why does control remain visible after postback?

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.Collecti ons.Generic
Partial Class _Default
Inherits System.Web.UI.P age
Friend dds As New List(Of DropDownList)

Protected Sub Page_PreInit(By Val sender As Object, ByVal e As
System.EventArg s) 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.AutoPostBac k = True
z1 = New ListItem("a", "a")
dd1.Items.Add(z 1)
z1 = New ListItem("b", "b")
dd1.Items.Add(z 1)

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

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.SelectedIndex Changed, AddressOf dropd
Next
End Sub

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

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

Protected Sub submit_Click(By Val sender As Object, ByVal e As
System.EventArg s)

End Sub
End Class
Jun 27 '08 #1
8 4483


"Dan" <d@ny.nlwrote in message
news:#V******** ******@TK2MSFTN GP06.phx.gbl...
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.Collecti ons.Generic
Partial Class _Default
Inherits System.Web.UI.P age
Friend dds As New List(Of DropDownList)

Protected Sub Page_PreInit(By Val sender As Object, ByVal e As
System.EventArg s) 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.AutoPostBac k = True
z1 = New ListItem("a", "a")
dd1.Items.Add(z 1)
z1 = New ListItem("b", "b")
dd1.Items.Add(z 1)

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

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.SelectedIndex Changed, AddressOf dropd
Next
End Sub

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

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

Protected Sub submit_Click(By Val sender As Object, ByVal e As
System.EventArg s)

End Sub
End Class
The ViewState field maintains this sort of information, it then recreates
the controls after postback based on their previous state.

--

Joe Fawcett (MVP - XML)
http://joe.fawcett.name

Jun 27 '08 #2
Dan
Hi, thanks for replying.

I put the property EnableViewState ="False"
but it makes no diffrerence: DD2 is still visible after clicking the button
....

Why?
"Joe Fawcett" <jo********@new sgroup.nospamsc hreef in bericht
news:CA******** *************** ***********@mic rosoft.com...
>

"Dan" <d@ny.nlwrote in message
news:#V******** ******@TK2MSFTN GP06.phx.gbl...
>Hi,

i experimented with postback and viewstate. With this code, there are 2
dropdownlist s 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.Collecti ons.Generic
Partial Class _Default
Inherits System.Web.UI.P age
Friend dds As New List(Of DropDownList)

Protected Sub Page_PreInit(By Val sender As Object, ByVal e As
System.EventAr gs) 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.AutoPostBac k = True
z1 = New ListItem("a", "a")
dd1.Items.Add(z 1)
z1 = New ListItem("b", "b")
dd1.Items.Add(z 1)

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

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.SelectedIndex Changed, AddressOf dropd
Next
End Sub

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

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

Protected Sub submit_Click(By Val sender As Object, ByVal e As
System.EventAr gs)

End Sub
End Class
The ViewState field maintains this sort of information, it then recreates
the controls after postback based on their previous state.

--

Joe Fawcett (MVP - XML)
http://joe.fawcett.name

Jun 27 '08 #3
Dan
Joe?
"Dan" <d@ny.nlschre ef in bericht
news:OA******** ******@TK2MSFTN GP02.phx.gbl...
Hi, thanks for replying.

I put the property EnableViewState ="False"
but it makes no diffrerence: DD2 is still visible after clicking the
button ...

Why?
"Joe Fawcett" <jo********@new sgroup.nospamsc hreef in bericht
news:CA******** *************** ***********@mic rosoft.com...
>>

"Dan" <d@ny.nlwrote in message
news:#V******* *******@TK2MSFT NGP06.phx.gbl.. .
>>Hi,

i experimented with postback and viewstate. With this code, there are 2
dropdownlis ts 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.Collecti ons.Generic
Partial Class _Default
Inherits System.Web.UI.P age
Friend dds As New List(Of DropDownList)

Protected Sub Page_PreInit(By Val sender As Object, ByVal e As
System.EventA rgs) 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.AutoPostBac k = True
z1 = New ListItem("a", "a")
dd1.Items.Add(z 1)
z1 = New ListItem("b", "b")
dd1.Items.Add(z 1)

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

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.SelectedIndex Changed, AddressOf dropd
Next
End Sub

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

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

Protected Sub submit_Click(By Val sender As Object, ByVal e As
System.EventA rgs)

End Sub
End Class
The ViewState field maintains this sort of information, it then recreates
the controls after postback based on their previous state.

--

Joe Fawcett (MVP - XML)
http://joe.fawcett.name


Jun 27 '08 #4


"Dan" <d@ny.nlwrote in message
news:uJ******** *****@TK2MSFTNG P02.phx.gbl...
Joe?
"Dan" <d@ny.nlschre ef in bericht
news:OA******** ******@TK2MSFTN GP02.phx.gbl...
>Hi, thanks for replying.

I put the property EnableViewState ="False"
but it makes no diffrerence: DD2 is still visible after clicking the
button ...

Why?
"Joe Fawcett" <jo********@new sgroup.nospamsc hreef in bericht
news:CA******* *************** ************@mi crosoft.com...
>>>

"Dan" <d@ny.nlwrote in message
news:#V****** ********@TK2MSF TNGP06.phx.gbl. ..
Hi,

i experimented with postback and viewstate. With this code, there are 2
dropdownlist s 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.Collecti ons.Generic
Partial Class _Default
Inherits System.Web.UI.P age
Friend dds As New List(Of DropDownList)

Protected Sub Page_PreInit(By Val sender As Object, ByVal e As
System.Event Args) 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.AutoPostBac k = True
z1 = New ListItem("a", "a")
dd1.Items.Add(z 1)
z1 = New ListItem("b", "b")
dd1.Items.Add(z 1)

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

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.SelectedIndex Changed, AddressOf dropd
Next
End Sub

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

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

Protected Sub submit_Click(By Val sender As Object, ByVal e As
System.Event Args)

End Sub
End Class

The ViewState field maintains this sort of information, it then
recreates the controls after postback based on their previous state.

--

Joe Fawcett (MVP - XML)
http://joe.fawcett.name


I tried to duplicate the situation but the second dropdown is not visible
after postback. The code is here:

TestViewState.a spx

<%@ Page Language="C#" AutoEventWireup ="true" CodeFile="Defau lt.aspx.cs"
Inherits="_Defa ult" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test ViewState</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownLi st ID="ddl1" runat="server" AutoPostBack="T rue"
Height="26px"
onselectedindex changed="ddl1_S electedIndexCha nged" Width="100px">
<asp:ListItem Selected="True" >a</asp:ListItem>
<asp:ListItem>b </asp:ListItem>
<asp:ListItem>c </asp:ListItem>
</asp:DropDownLis t>&nbsp;
<asp:DropDownLi st ID="ddl2" runat="server" EnableViewState ="False"
Height="31px" Visible="False" Width="100px">
<asp:ListItem Selected="True" >1</asp:ListItem>
<asp:ListItem>2 </asp:ListItem>
<asp:ListItem>3 </asp:ListItem>
</asp:DropDownLis t>&nbsp;<asp:Bu tton ID="cmd1" runat="server"
Text="Postback" />
</div>
</form>
</body>
</html>
TestViewState.a spx.cs
using System;

public partial class _Default : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{

}
protected void ddl1_SelectedIn dexChanged(obje ct sender, EventArgs e)
{
if (ddl1.SelectedV alue.ToString() == "b")
ddl2.Visible = true;
else
ddl2.Visible = false;
}
}
--

Joe Fawcett (MVP - XML)
http://joe.fawcett.name

Jun 27 '08 #5
Dan
Thanks again,

but there is a big difference between your code and mine: you define the
dropdownlists in the aspx file, i define everything in code-behind.

I certify that with this code below as it is, when both DDLs are visible and
when you then click on the submit button, both DDL remains visible.
And i tried with EnableViewState ="true" and with EnableViewState ="false".

Just copy and paste it in your IIS and try.

So i have still the same question: what makes that DDL remaining visible?

Imports System.Collecti ons.Generic
Partial Class _Default
Inherits System.Web.UI.P age
Friend dds As New List(Of DropDownList)

Protected Sub Page_PreInit(By Val sender As Object, ByVal e As
System.EventArg s) Handles Me.PreInit
Dim dd1, dd2 As DropDownList
Dim z1, z2 As ListItem
If Not IsPostBack Then
dd1 = New DropDownList
dd1.ID = "dd1"
dd1.AutoPostBac k = True
z1 = New ListItem("a", "a")
dd1.Items.Add(z 1)
z1 = New ListItem("b", "b")
dd1.Items.Add(z 1)

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

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.SelectedIndex Changed, AddressOf dropd
Next
End Sub

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

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

Protected Sub submit_Click(By Val sender As Object, ByVal e As
System.EventArg s)
End Sub
End Class

----------------------------------------------------------------------------
Jun 27 '08 #6

"Dan" <d@ny.nlwrote in message
news:Og******** ******@TK2MSFTN GP05.phx.gbl...
Thanks again,

but there is a big difference between your code and mine: you define the
dropdownlists in the aspx file, i define everything in code-behind.

I certify that with this code below as it is, when both DDLs are visible
and when you then click on the submit button, both DDL remains visible.
And i tried with EnableViewState ="true" and with EnableViewState ="false".

Just copy and paste it in your IIS and try.

So i have still the same question: what makes that DDL remaining visible?

Imports System.Collecti ons.Generic
Partial Class _Default
Inherits System.Web.UI.P age
Friend dds As New List(Of DropDownList)

Protected Sub Page_PreInit(By Val sender As Object, ByVal e As
System.EventArg s) Handles Me.PreInit
Dim dd1, dd2 As DropDownList
Dim z1, z2 As ListItem
If Not IsPostBack Then
dd1 = New DropDownList
dd1.ID = "dd1"
dd1.AutoPostBac k = True
z1 = New ListItem("a", "a")
dd1.Items.Add(z 1)
z1 = New ListItem("b", "b")
dd1.Items.Add(z 1)

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

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.SelectedIndex Changed, AddressOf dropd
Next
End Sub

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

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

Protected Sub submit_Click(By Val sender As Object, ByVal e As
System.EventArg s)
End Sub
End Class

----------------------------------------------------------------------------

You are not declaring the second drop down as AutoPostBack = True

Also if you turn Strict On ... (always a good thing to do to catch
problems), when you do you will see that you are trying to set a boolean to
"false". Those are the types of things that will bite you in the end.

Hope this helps
LS

Jun 27 '08 #7
Dan
The second DDL has not its property AutoPostBack set on true, indeed, but
thus has nothing to do with the fact DD2 remains visibel after clicking the
submit button.

I tried the code with Strict on and off, but this makes no difference here
....

So, i still have the same question unsolved.
A great mystery in ASP.NET land or something stupid nobody sees?
"Lloyd Sheen" <a@b.cschreef in bericht
news:uU******** ******@TK2MSFTN GP04.phx.gbl...
>
"Dan" <d@ny.nlwrote in message
news:Og******** ******@TK2MSFTN GP05.phx.gbl...
>Thanks again,

but there is a big difference between your code and mine: you define the
dropdownlist s in the aspx file, i define everything in code-behind.

I certify that with this code below as it is, when both DDLs are visible
and when you then click on the submit button, both DDL remains visible.
And i tried with EnableViewState ="true" and with EnableViewState ="false".

Just copy and paste it in your IIS and try.

So i have still the same question: what makes that DDL remaining visible?

Imports System.Collecti ons.Generic
Partial Class _Default
Inherits System.Web.UI.P age
Friend dds As New List(Of DropDownList)

Protected Sub Page_PreInit(By Val sender As Object, ByVal e As
System.EventAr gs) Handles Me.PreInit
Dim dd1, dd2 As DropDownList
Dim z1, z2 As ListItem
If Not IsPostBack Then
dd1 = New DropDownList
dd1.ID = "dd1"
dd1.AutoPostBac k = True
z1 = New ListItem("a", "a")
dd1.Items.Add(z 1)
z1 = New ListItem("b", "b")
dd1.Items.Add(z 1)

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

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.SelectedIndex Changed, AddressOf dropd
Next
End Sub

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

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

Protected Sub submit_Click(By Val sender As Object, ByVal e As
System.EventAr gs)
End Sub
End Class

----------------------------------------------------------------------------


You are not declaring the second drop down as AutoPostBack = True

Also if you turn Strict On ... (always a good thing to do to catch
problems), when you do you will see that you are trying to set a boolean
to "false". Those are the types of things that will bite you in the end.

Hope this helps
LS

Jun 27 '08 #8
Dan,

Out of interest, why are you defining all the controls programatically ?
Wouldn't it be much easier just to put them on the ASPX page and
show/hide them if you need to using the "visible" property?

It seems a very strange and complicated way to do what you're doing, but
I presume you have a good reason? :)

Nick..

Dan wrote:
The second DDL has not its property AutoPostBack set on true, indeed, but
thus has nothing to do with the fact DD2 remains visibel after clicking the
submit button.

I tried the code with Strict on and off, but this makes no difference here
...

So, i still have the same question unsolved.
A great mystery in ASP.NET land or something stupid nobody sees?
"Lloyd Sheen" <a@b.cschreef in bericht
news:uU******** ******@TK2MSFTN GP04.phx.gbl...
>"Dan" <d@ny.nlwrote in message
news:Og******* *******@TK2MSFT NGP05.phx.gbl.. .
>>Thanks again,

but there is a big difference between your code and mine: you define the
dropdownlis ts in the aspx file, i define everything in code-behind.

I certify that with this code below as it is, when both DDLs are visible
and when you then click on the submit button, both DDL remains visible.
And i tried with EnableViewState ="true" and with EnableViewState ="false".

Just copy and paste it in your IIS and try.

So i have still the same question: what makes that DDL remaining visible?

Imports System.Collecti ons.Generic
Partial Class _Default
Inherits System.Web.UI.P age
Friend dds As New List(Of DropDownList)

Protected Sub Page_PreInit(By Val sender As Object, ByVal e As
System.EventA rgs) Handles Me.PreInit
Dim dd1, dd2 As DropDownList
Dim z1, z2 As ListItem
If Not IsPostBack Then
dd1 = New DropDownList
dd1.ID = "dd1"
dd1.AutoPostBac k = True
z1 = New ListItem("a", "a")
dd1.Items.Add(z 1)
z1 = New ListItem("b", "b")
dd1.Items.Add(z 1)

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

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.SelectedIndex Changed, AddressOf dropd
Next
End Sub

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

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

Protected Sub submit_Click(By Val sender As Object, ByVal e As
System.EventA rgs)
End Sub
End Class

----------------------------------------------------------------------------

You are not declaring the second drop down as AutoPostBack = True

Also if you turn Strict On ... (always a good thing to do to catch
problems), when you do you will see that you are trying to set a boolean
to "false". Those are the types of things that will bite you in the end.

Hope this helps
LS

Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
408
by: kai | last post by:
Hi, All I create an ASP.NET page, it contains FirstName textbox and LastName textbox. I setup "enableViewState=false" in page directive. When I enter data in FirstName and LastName textbox, after I refresh the page or go to different page and come back to the same page, I find data entered in FirstName and LastName textbox are still there. I think after setup "enableViewState=false" , we are not suppose to see data in the textbox...
3
1939
by: Dennis M | last post by:
Hey everyone, I am curious what the performance impact of a custom control would be if it had a significant hierarchy of children. For example, 40 child controls, 5 levels deep, each control on average has 20 properties. What would be the best way to implement such a thing? The book I am reading uses templated controls and basically reconstructs the entire hierarchy everytime page is accessed based on the tags in the .aspx file, but...
1
2693
by: Gunjan Garg | last post by:
Hello All, I am working to create a generic datagrid which accepts a datasource(ListData - This is our own datatype) and depending on the calling program customizes itself for sorting, paginantion or accepting the add and remove item events. What i am observing is that none of the vents are happening... (Sort, page, or item). I am sure I am missing something basic here... Need help... Thanks much
2
1137
by: Seok Bee | last post by:
Dear Experts, How can I remain the value of other controls when I enter other value in another control with POSTBACK attribute is set to true. Currently, when I selected a date from a date picker into a textbox control, after that I enter a value into another textbox control with POSTBACK attribute is True. Once the page is refresh, the textbox value of the date selected is reset to empty again. How can I remain the value in the control?...
0
1319
by: Don | last post by:
I have a page that has several controls on it and the controls are shown or hidden during postback. I show and hide them using the visible property. When I use 'view source' in Internet Explorer I always see the source of the first version of the page, with the controls rendered that where rendered on the first visit. The changes that occur during postback are not reflected in 'view source'. Example Detail: My page has 6 controls and...
1
2152
by: clintonG | last post by:
I'm having a problem maintaining state with a Panel control in a MasterPage and I need help thinking through this process. The basic structure of the HTML in the Master looks like this... <asp:PanelActivatorLinkButton ... /> .... .... <asp:Panel Visible="" ...> <asp:LoadContentLinkButton ... /> <asp:ContentPlaceHolder ... />
6
11136
by: AG | last post by:
I have a gridview with a template column containing an imagebutton to delete the row. Under some condition I don't want the row to be deleted, so would like to remove the button. In the RowDataBound event, I can find the button, but can't seem to remove it. The code below does not throw any exception, but the button is not removed. Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As
1
3117
by: realfantasy | last post by:
i made asp.net webusercontrol which contains panel which contains datalist and that panel is in ajax collapsableExtender and that webusercontrol is in master page header and in always visible control problem is little i think but very stupid that whenever i go on any page whilst page load my webusercontrol expand for a sec and then collapse but i want it to remain collapsed until i want it to expand.. you may see demo on...
1
2901
by: realfantasy | last post by:
i made asp.net webusercontrol which contains panel which contains datalist and that panel is in ajax collapsableExtender and that webusercontrol is in master page header and in always visible control problem is little i think but very stupid that whenever i go on any page whilst page load my webusercontrol expand for a sec and then collapse but i want it to remain collapsed until i want it to expand.. you may see demo on...
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.