Solved

Enable Disable Drop Down

Posted on 2010-08-20
7
725 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

777 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