?
Solved

Enable Disable Drop Down

Posted on 2010-08-20
7
Medium Priority
?
729 Views
Last Modified: 2012-05-10
Hi Experts,
I want to enable/disable a drop down on C#.  I am able to do it in Java Script.  But, I need to do it on C# too during page load and on click of Submit button.  Below is my code.  I have three drop downs.  I need to enable the 3rd drop down i.e. id="OrgUnit" if WorkCountry = "DE" selected. For other countries, it should be disabled.  Please advice.  
Thank you in advance.


<html>
      <head>
            <script type="text/javascript">
            function makeDisable(){
                  var x=document.getElementById("mySelect")
                  x.disabled=true
            }
            function makeEnable(){
                  var x=document.getElementById("mySelect")
                  x.disabled=false
            }
            function EnableOrgUnit(){
                  var x=document.getElementById("WorkCountry")
                  var y=document.getElementById("lblOrgUnit")
                  var z=document.getElementById("OrgUnit")
                alert('Working: ' + x.value);
                  if (x.value == "DE")
                  {
                        y.disabled=false
                        z.disabled=false
                  }
                  else
                  {
                        y.disabled=true
                        z.disabled=true
                  }
            }
            </script>
      </head>
      <body>
            <table cellSpacing="0" cellPadding="0" width="400" border="0">
                  <form id="Form1" runat="server">
                        <TBODY>
                              <tr>
                                    <td>
                                          <asp:dropdownlist id="mySelect" tabIndex="15" runat="server" Width="150px">
                                                <asp:ListItem Value="Apple" Selected="True">Apple</asp:ListItem>
                                                <asp:ListItem Value="Banana">Banana</asp:ListItem>
                                                <asp:ListItem Value="Orange">Orange</asp:ListItem>
                                          </asp:dropdownlist>
                                    </td>
                                    <td><input type="button" onclick="makeDisable()" value="Disable list"></td>
                                    <td><input type="button" onclick="makeEnable()" value="Enable List"></td>
                              </tr>
                              <tr>
                                    <td width="118" style="WIDTH: 118px"><FONT class="label">Work Country:</FONT><span class="requiredField">&nbsp;*</span></td>
                                    <td width="70%"><asp:dropdownlist class="Required" id="WorkCountry" tabIndex="14" runat="server" Width="120px" onchange="EnableOrgUnit();">
                                                <asp:ListItem Value="No Selection" Selected="True">--- Select one ---</asp:ListItem>
                                                <asp:ListItem Value="US">US</asp:ListItem>
                                                <asp:ListItem Value="DE">DE</asp:ListItem>
                                          </asp:dropdownlist></td>
                              </tr>
                              <tr>
                                    <td width="118" style="WIDTH: 118px"><span class="requiredField">
                                                <asp:Label id="lblOrgUnit" runat="server" Font-Bold="True" ForeColor="#1A659E">Org Unit:</asp:Label></span></td>
                                    <td width="70%"><asp:dropdownlist id="OrgUnit" tabIndex="15" runat="server" Width="150px">
                                                <asp:ListItem Value="No Selection" Selected="True">--- Select one ---</asp:ListItem>
                                                <asp:ListItem Value="Aerospace Lubeck">Aerospace Lubeck</asp:ListItem>
                                                <asp:ListItem Value="Aerospace Hamburg">Aerospace Hamburg</asp:ListItem>
                                                <asp:ListItem Value="Aerospace Finkenwerder">Aerospace Finkenwerder</asp:ListItem>
                                          </asp:dropdownlist></td>
                              </tr>
                  </form>
                  </TBODY>
            </table>
      </body>
</html>
0
Comment
Question by:RadhaKrishnaKiJaya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 57

Assisted Solution

by:HainKurt
HainKurt earned 800 total points
ID: 33490232
how do you do in your js?

do the same thing with c#

dropdown.attributes.add("disabled", "disabled");
0
 
LVL 57

Accepted Solution

by:
HainKurt earned 800 total points
ID: 33490235
or use

dropdown.Enabled=false;
0
 
LVL 5

Assisted Solution

by:greatsubash
greatsubash earned 400 total points
ID: 33490237
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 27

Assisted Solution

by:nmarun
nmarun earned 400 total points
ID: 33490243
Add the property 'AutoPostBack=True' and get a handle for the event OnSelectedIndexChanged on the WorkCountry control

http://www.devasp.net/net/articles/display/678.html

So when the user changes an entry on the WorkCountry control, the page posts back and your code will get executed.

Arun
0
 
LVL 16

Assisted Solution

by:Vikram Singh Saini
Vikram Singh Saini earned 400 total points
ID: 33491212
Hi,

Please check code.

Regards,
VSS

--------------------------
// Default.aspx
--------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Disable DropDownList</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        <p>Hi Experts,
I want to enable/disable a drop down on C#.  I am able to do it in Java Script.  But, I need to do it on C# too during page load and on click of Submit button.  Below is my code.  I have three drop downs.  I need to enable the 3rd drop down i.e. id="OrgUnit" if WorkCountry = "DE" selected. For other countries, it should be disabled.  Please advice.  
Thank you in advance.</p>
            <table cellspacing="0" cellpadding="0" width="400" border="0">
                <tbody>
                    <tr>
                        <td>
                            <asp:DropDownList ID="mySelect" TabIndex="15" runat="server" Width="150px" AutoPostBack="true">
                                <asp:ListItem Value="Apple" Selected="True">Apple</asp:ListItem>
                                <asp:ListItem Value="Banana">Banana</asp:ListItem>
                                <asp:ListItem Value="Orange">Orange</asp:ListItem>
                            </asp:DropDownList>
                        </td>
                        <td>
                        <asp:Button ID="Button1" runat="Server" Text="Disable list" OnClick="makeDisable" /></td>
                        <td>
                           <asp:Button ID="Button2" runat="Server" Text="Enable list" OnClick="makeEnable" /></td>
                    </tr>
                    <tr>
                        <td width="118" style="width: 118px">
                            <font class="label">Work Country:</font><span class="requiredField">&nbsp;*</span></td>
                        <td width="70%">
                            <asp:DropDownList ID="WorkCountry" TabIndex="14" runat="server"
                                Width="120px" OnSelectedIndexChanged="WorkCountry_SelectedIndexChanged" AutoPostBack="true">
                                <asp:ListItem Value="No Selection" Selected="True">--- Select one ---</asp:ListItem>
                                <asp:ListItem Value="US">US</asp:ListItem>
                                <asp:ListItem Value="DE">DE</asp:ListItem>
                            </asp:DropDownList></td>
                    </tr>
                    <tr>
                        <td width="118" style="width: 118px">
                            <span class="requiredField">
                                <asp:Label ID="lblOrgUnit" runat="server" Font-Bold="True" ForeColor="#1A659E">Org Unit:</asp:Label></span></td>
                        <td width="70%">
                            <asp:DropDownList ID="OrgUnit" TabIndex="15" runat="server" Width="150px" AutoPostBack="true">
                                <asp:ListItem Value="No Selection" Selected="True">--- Select one ---</asp:ListItem>
                                <asp:ListItem Value="Aerospace Lubeck">Aerospace Lubeck</asp:ListItem>
                                <asp:ListItem Value="Aerospace Hamburg">Aerospace Hamburg</asp:ListItem>
                                <asp:ListItem Value="Aerospace Finkenwerder">Aerospace Finkenwerder</asp:ListItem>
                            </asp:DropDownList></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </form>
</body>
</html>

---------------------------------
// Default.aspx.cs
---------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Modify this as per your requirements
        mySelect.Enabled = false;
        OrgUnit.Enabled = false;
    }
    protected void WorkCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (WorkCountry.SelectedItem.Value == "DE")
        {
            OrgUnit.Enabled = true;
        }
        else
        {
            OrgUnit.Enabled = false;
        }
    }

    protected void makeEnable(object sender, EventArgs e)
    {
        mySelect.Enabled = true;
    }

    protected void makeDisable(object sender, EventArgs e)
    {
        mySelect.Enabled = false;
    }   
}

Open in new window

0
 

Author Closing Comment

by:RadhaKrishnaKiJaya
ID: 33492272
Hi VSS, Arun, greatsubash, HainKur,
Thank you all for your wonderful help.  It worked great in no time.  VSS, thank you again for taking my code and working on it.  A simple thank is not enough for the help you did at the time of need. Plese keep doing so.  Thank you again.  Bye now.
0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 33492280
This solution deserves 9 out of 10.  I don't know how it is calaculated to 7.6.  Thanks again.
0

Featured Post

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question