Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. visibility attribute

visibility attribute

Scheduled Pinned Locked Moved ASP.NET
questioncsharpasp-net
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mr Cully
    wrote on last edited by
    #1

    I think this is a simple question but Im new to asp.net so give me a break!!! i have a drop down menu and i want to make different labels visible when different values are selected from the dropdown menu, i dont wan to have to use a button to submit tho, any suggestions?

    M 1 Reply Last reply
    0
    • M Mr Cully

      I think this is a simple question but Im new to asp.net so give me a break!!! i have a drop down menu and i want to make different labels visible when different values are selected from the dropdown menu, i dont wan to have to use a button to submit tho, any suggestions?

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      You can set the AutoPostBack attribute of the DropDownList control to true, then code for the SelectedIndexChanged event. Something like this should work:

      <% @Page Language="c#" %>
      <script runat="server">
      void ddSelectionChange(Object sender, EventArgs e)
      {
      // hide all labels to start with
      lbl1.Visible = false;
      lbl2.Visible = false;
      lbl3.Visible = false;

      // determine the value of the selected drop down list item
      string sID = dd.SelectedItem.Value;
      
      // make that label visible
      if (sID != null && sID != "")
      {
          Control c = FindControl(sID);
          c.Visible = true;
      }
      

      }
      </script>

      <html>
      <head></head>
      <body>

      <form runat="server">
          <asp:DropDownList id="dd" runat="server"
                            AutoPostBack="True"
                            OnSelectedIndexChanged="ddSelectionChange" >
          
              <asp:ListItem Value="lbl1" Text="First Label" Selected="True" />
              <asp:ListItem Value="lbl2" Text="Second Label" />
              <asp:ListItem Value="lbl3" Text="Third Label" />
              
          </asp:DropDownList>
          
          <asp:Label id="lbl1" runat="server" visible="true"
                     text="This is Label 1" />
      
          <asp:Label id="lbl2" runat="server" visible="false"
                     text="This is Label 2" />
      
          <asp:Label id="lbl3" runat="server" visible="false"
                     text="This is Label 3" />
          
      </form>
      

      </body>
      </html>

      M 1 Reply Last reply
      0
      • M Mike Ellison

        You can set the AutoPostBack attribute of the DropDownList control to true, then code for the SelectedIndexChanged event. Something like this should work:

        <% @Page Language="c#" %>
        <script runat="server">
        void ddSelectionChange(Object sender, EventArgs e)
        {
        // hide all labels to start with
        lbl1.Visible = false;
        lbl2.Visible = false;
        lbl3.Visible = false;

        // determine the value of the selected drop down list item
        string sID = dd.SelectedItem.Value;
        
        // make that label visible
        if (sID != null && sID != "")
        {
            Control c = FindControl(sID);
            c.Visible = true;
        }
        

        }
        </script>

        <html>
        <head></head>
        <body>

        <form runat="server">
            <asp:DropDownList id="dd" runat="server"
                              AutoPostBack="True"
                              OnSelectedIndexChanged="ddSelectionChange" >
            
                <asp:ListItem Value="lbl1" Text="First Label" Selected="True" />
                <asp:ListItem Value="lbl2" Text="Second Label" />
                <asp:ListItem Value="lbl3" Text="Third Label" />
                
            </asp:DropDownList>
            
            <asp:Label id="lbl1" runat="server" visible="true"
                       text="This is Label 1" />
        
            <asp:Label id="lbl2" runat="server" visible="false"
                       text="This is Label 2" />
        
            <asp:Label id="lbl3" runat="server" visible="false"
                       text="This is Label 3" />
            
        </form>
        

        </body>
        </html>

        M Offline
        M Offline
        Mr Cully
        wrote on last edited by
        #3

        ah i see great thanks, i forgot to change the autopostback attribute to true

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups