crosstf
asked on
ASP.NET Dropdownlist Control, Javascript, Disable Textbox based on selected value
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_uni t" runat="server" CssClass="gv_ddl" OnSelectedIndexChanged="dd l_empty_ra te_per_uni t_Selected IndexChang ed" AutoPostBack="true" >
<asp:ListItem></asp:ListIt em>
<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>
<asp:TextBox ID="txt_empty_rate_per_oth er_unit_de scription" runat="server" MaxLength="25" Width="70px" BorderColor="Black" BorderWidth="1px"></asp:Te xtBox></di v>
How can I do this so when the user simply selects Per Other, that text box is enabled?
<asp:DropDownList ID="ddl_empty_rate_per_uni
<asp:ListItem></asp:ListIt
<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>
<asp:TextBox ID="txt_empty_rate_per_oth
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;
}
}
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;
}
}
what is the problem with second approach (C#)? are you getting any error ?
ASKER
It doesn't disable the textbox simply by selecting it. I need javascript I believe.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I figured it out. A link to the solution is provided.