Solved

Enable Disable Drop Down

Posted on 2010-08-20
7
721 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
7 Comments
 
LVL 51

Assisted Solution

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

do the same thing with c#

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

Accepted Solution

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

dropdown.Enabled=false;
0
 
LVL 5

Assisted Solution

by:greatsubash
greatsubash earned 100 total points
ID: 33490237
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 27

Assisted Solution

by:nmarun
nmarun earned 100 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 100 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Re-imbursement Claim System 3 25
Chat Room 1 29
COnsume rest client 6 12
Red error squiggly on vb.net 7 12
Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

759 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now