why does control remain visible after postback?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dan

    why does control remain visible after 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?
    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


  • Joe Fawcett

    #2
    Re: why does control remain visible after postback?



    "Dan" <d@ny.nlwrote in message
    news:#VdOHHnvIH A.4376@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)


    Comment

    • Dan

      #3
      Re: why does control remain visible after postback?

      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" <joefawcett@new sgroup.nospamsc hreef in bericht
      news:CA1A3E19-D310-4123-899A-459577547F51@mi crosoft.com...
      >
      >
      "Dan" <d@ny.nlwrote in message
      news:#VdOHHnvIH A.4376@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

      Comment

      • Dan

        #4
        Re: why does control remain visible after postback?

        Joe?


        "Dan" <d@ny.nlschre ef in bericht
        news:OAZoDhpvIH A.5832@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" <joefawcett@new sgroup.nospamsc hreef in bericht
        news:CA1A3E19-D310-4123-899A-459577547F51@mi crosoft.com...
        >>
        >>
        >"Dan" <d@ny.nlwrote in message
        >news:#VdOHHnvI HA.4376@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
        >
        >

        Comment

        • Joe Fawcett

          #5
          Re: why does control remain visible after postback?



          "Dan" <d@ny.nlwrote in message
          news:uJZngg0vIH A.420@TK2MSFTNG P02.phx.gbl...
          Joe?
          >
          >
          "Dan" <d@ny.nlschre ef in bericht
          news:OAZoDhpvIH A.5832@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" <joefawcett@new sgroup.nospamsc hreef in bericht
          >news:CA1A3E1 9-D310-4123-899A-459577547F51@mi crosoft.com...
          >>>
          >>>
          >>"Dan" <d@ny.nlwrote in message
          >>news:#VdOHHnv IHA.4376@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)


          Comment

          • Dan

            #6
            Re: why does control remain visible after postback?

            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

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


            Comment

            • Lloyd Sheen

              #7
              Re: why does control remain visible after postback?


              "Dan" <d@ny.nlwrote in message
              news:OgDHfBDwIH A.2068@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

              Comment

              • Dan

                #8
                Re: why does control remain visible after postback?

                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:uUXb82DwIH A.5448@TK2MSFTN GP04.phx.gbl...
                >
                "Dan" <d@ny.nlwrote in message
                news:OgDHfBDwIH A.2068@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

                Comment

                • Nick Gilbert

                  #9
                  Re: why does control remain visible after postback?

                  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:uUXb82DwIH A.5448@TK2MSFTN GP04.phx.gbl...
                  >"Dan" <d@ny.nlwrote in message
                  >news:OgDHfBDwI HA.2068@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
                  >
                  >

                  Comment

                  Working...