• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 731
  • Last Modified:

Enable Disable Drop Down

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
RadhaKrishnaKiJaya
Asked:
RadhaKrishnaKiJaya
5 Solutions
 
HainKurtSr. System AnalystCommented:
how do you do in your js?

do the same thing with c#

dropdown.attributes.add("disabled", "disabled");
0
 
HainKurtSr. System AnalystCommented:
or use

dropdown.Enabled=false;
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
nmarunCommented:
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
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
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
 
RadhaKrishnaKiJayaAuthor Commented:
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
 
RadhaKrishnaKiJayaAuthor Commented:
This solution deserves 9 out of 10.  I don't know how it is calaculated to 7.6.  Thanks again.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now