Solved

Enable Disable Drop Down

Posted on 2010-08-20
7
727 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 51

Assisted Solution

by:Huseyin KAHRAMAN
Huseyin KAHRAMAN 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:
Huseyin KAHRAMAN 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Linq - Join two objects into one 3 35
Coding for the first time 9 69
ModalPopup  question 22 38
Connect Gridview column to Textbox in C# 2 41
If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
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.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

733 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