Click here to Skip to main content
16,004,782 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
XML
<asp:CheckBoxList ID="AllowencesCheckbox" runat="server" AutoPostBack="true" "">
                                           <asp:ListItem Text="hi" Value="1"></asp:ListItem>
                                           <asp:ListItem Text="hello" Value="2"></asp:ListItem>
                                       </asp:CheckBoxList>




XML
<asp:DropDownList ID="MileageRateDropDownList" runat="server" 
                                           Visible="false">
                                        </asp:DropDownList>


here if i select "hi" then MileageRateDropDownListis set to visible true using javascript
Posted
Updated 11-Mar-13 1:21am
v4

Try this code

In code behind

C#
protected void Page_Load(object sender, EventArgs e)
    {
        foreach (ListItem item in AllowencesCheckbox.Items)
        {
            if (item.Value == "1")
            {
                item.Attributes.Add("onchange", "Display(document.activeElement.checked);");   
            }
        }
    }



In design


JavaScript
<script type="text/javascript" language="javascript">

        function Display(val) {

            var objDDL = document.getElementById('<%= MileageRateDropDownList.ClientID %>');

            if (val == false) {
                objDDL.style.display = "none";
                objDDL.style.visibility = "hidden";
            }
            else {
                objDDL.style.display = "block";
                objDDL.style.visibility = "visible";
            }
        }
    </script>

ASP.NET
<asp:checkboxlist id="AllowencesCheckbox" runat="server" autopostback="false" xmlns:asp="#unknown">
        <asp:listitem text="hi" value="1"></asp:listitem>
        <asp:listitem text="hello" value="2"></asp:listitem>
    </asp:checkboxlist>
    <asp:dropdownlist id="MileageRateDropDownList" runat="server" xmlns:asp="#unknown">
    </asp:dropdownlist>
 
Share this answer
 
i would remove the Visible="false" of the ddl to manage visibility only js side (jQuery)

JavaScript
$(document).ready(function() {
    // hide dropdownlist
    $('select#"<%=MileageRateDropDownList.ClientID%>"').hide();
    
    // loop checkboxes cheched
    $('input[id*="AllowencesCheckbox"]:checked').each(function() {
      
      // if "hi" 
      if($(this).val() == "1")  // not sure, try 1 or "1"
      {
        // show
        $('select#"<%=MileageRateDropDownList.ClientID%>"').show();
      }
    });
   
});


NOTE it's untested..

you can replace show with .slideDown() may be is cuter :)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900