Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP.NET Dropdownlist Control, Javascript, Disable Textbox based on selected value

Posted on 2011-03-15
4
Medium Priority
?
2,230 Views
Last Modified: 2012-05-11
I am using an asp.net gridview. In it there is a dropdownlist control. I want to be able to disable the textbox below it based on the selection in the the dropdownlist. So if the value "Other" is selected, enable that texbox, otherwise, disable it. I have tried two different approaches, both have failed. The first, was to use javascript, but that failed because the  ids change when there are multiple grids, so hard-coding by ID only works for one of those instances.  The second approach was to use the on selected index changed event of the dropdownlist:

<asp:DropDownList ID="ddl_empty_rate_per_unit" runat="server" CssClass="gv_ddl" OnSelectedIndexChanged="ddl_empty_rate_per_unit_SelectedIndexChanged" AutoPostBack="true" >
<asp:ListItem></asp:ListItem>
                            <asp:ListItem Value="Hour">Per Hour</asp:ListItem>
                            <asp:ListItem Value="Day">Per Day</asp:ListItem>
                            <asp:ListItem Value="Week">Per Week</asp:ListItem>
                            <asp:ListItem Value="Month">Per Month</asp:ListItem>
                            <asp:ListItem Value="Other">Per Other</asp:ListItem>
                            </asp:DropDownList>
                            &nbsp;
                                <asp:TextBox ID="txt_empty_rate_per_other_unit_description" runat="server" MaxLength="25" Width="70px" BorderColor="Black" BorderWidth="1px"></asp:TextBox></div>


How can I do this so when the user simply selects Per Other, that text box is enabled?



Javascript code: 
 function rpu(ddl) {

        var ddlval = document.getElementById("ctl00_cph_Main_F144_R2002_01_gecx_F144_GridView1_ctl01_ddl_empty_rate_per_unit").value
        if (ddlval == "Other") {

            document.getElementById("ctl00_cph_Main_F144_R2002_01_gecx_F144_GridView1_ctl01_txt_empty_rate_per_other_unit_description").disabled = false;
        } else {

            document.getElementById("ctl00_cph_Main_F144_R2002_01_gecx_F144_GridView1_ctl01_txt_empty_rate_per_other_unit_description").disabled = true;

        }
    }

Open in new window

C Sharp Method:

 protected void ddl_empty_rate_per_unit_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl_empty_rate_per_unit = GridView1.FindControl("ddl_empty_rate_per_unit") as DropDownList;
        TextBox txt_empty_rate_per_other_unit_description = GridView1.FindControl("txt_empty_rate_per_other_unit_description") as TextBox;

        if (ddl_empty_rate_per_unit.SelectedValue == "Other")
        {
            txt_empty_rate_per_other_unit_description.Enabled = true;

        }
        else
        {
            txt_empty_rate_per_other_unit_description.Enabled = false;
        }




    }

Open in new window

0
Comment
Question by:crosstf
[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
  • 3
4 Comments
 
LVL 9

Expert Comment

by:gery128
ID: 35138096
what is the problem with second approach (C#)? are you getting any error ?
0
 

Author Comment

by:crosstf
ID: 35138185
It doesn't disable the textbox simply by selecting it. I need javascript I believe.
0
 

Accepted Solution

by:
crosstf earned 0 total points
ID: 35140270
I figured it out. Thanks.

Here is the solution:

http://forums.asp.net/t/1411628.aspx


0
 

Author Closing Comment

by:crosstf
ID: 35174544
I figured it out. A link to the solution is provided.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

722 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