Javascript Enable/Disable Dropdown in Gridview using Checkboxes

If you come up with some requirement in which you have a Checkbox and a DropDownList in GridView control and you are requried to enable disable the DropDownList on the basis of the CheckBox value you can use following Code.


-----------------------------------.aspx file ----------------------------------

<script type="text/javascript">
        function StatusGridDropdown(drpdown)
        {
            document.getElementById(drpdown).disabled = !document.getElementById(drpdown).disabled;
        }
</script>
.
.
.
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" Width="400px">
        <Columns>
            <asp:TemplateField>
                <AlternatingItemTemplate>
                    <asp:CheckBox ID="cbStatus" runat="server" />
                </AlternatingItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="cbStatus" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <AlternatingItemTemplate>
                    <asp:DropDownList ID="ddl" runat="server" Enabled="False" Width="169px">
                    </asp:DropDownList>
                </AlternatingItemTemplate>
                <ItemTemplate>
                    <asp:DropDownList ID="ddl" runat="server" Width="169px" Enabled="False">
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
</asp:GridView>


----------------------------------.vb file if VB.Net is the language ------------------------------------

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            DirectCast(e.Row.FindControl("cbStatus"), CheckBox).Attributes.Add("onclick", "javascript:StatusGridDropdown('" & DirectCast(e.Row.FindControl("ddl"), DropDownList).ClientID & "')")
        End If
End Sub


----------------------------------.cs file if C#.Net is the language ------------------------------------

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if ((e.Row.RowType == DataControlRowType.DataRow)) {
        ((CheckBox)e.Row.FindControl("cbStatus")).Attributes.Add("onclick", "javascript:StatusGridDropdown('" + ((DropDownList)e.Row.FindControl("ddl")).ClientID + "')");
    }
}

30 Comments

  • Thanks Alot Faraz ,
    I am new to javascript and asp.net how can we change this one if we have one more textbox and want to disable the dropdown and checkbox in single checkbox click.
    thanks.
    try to add something like this only work for textbox.
    If (e.Row.RowType = DataControlRowType.DataRow) Then
    DirectCast(e.Row.FindControl("cbStatus"), CheckBox).Attributes.Add("onclick", "javascript:StatusGridDropdown('" & DirectCast(e.Row.FindControl("ddl"), DropDownList).ClientID & "')")
    DirectCast(e.Row.FindControl("cbStatus"), CheckBox).Attributes.Add("onclick", "javascript:StatusGridDropdown('" & DirectCast(e.Row.FindControl("TextBox1"), TextBox).ClientID & "')")
    End If

  • what i mean single checkbox click to handle enable or disable two controls inside the gridview.

  • greate help me thanks for it

  • how can we use the same in formview?

  • i have one checkbox and two dropdownlists .If checkbox checked both should be disabled.
    one dropdownlist is like

    states

    and other cities in that particular state

  • Thanks it helped me to solve the query

  • Will this work to disable a button if a checkbox is checked? Do I just replace the dropdownlist name with the button name instead?

    What I am trying to do is prevent users from deleting a row in the gridview if it is locked (via the checkbox control).

  • @Skye

    Well yes it should work, just try to replace the dropdown with Button and make sure that while passing the ClientID cast the control with Button type instead of DropDownList type.

  • I've a problem, if the textbox are enabled=false that solution d'ont work. the solution only work if the textbox is enabled = true.
    i d'ont no the solution. any help?

  • Ekdam Zhakassss!!!!!!!!!!!
    It helped me a lot.
    Thanks a lot!

  • Greate Simple piece of code...........

  • How can I adjust this so that if a checkbox within the gridview is checked it disables a button that is on the webform and not within the gridview?

  • @HMcoder:

    Well if you want to disable a button outside the gridview its not a problem. you can use this code

    ((CheckBox)e.Row.FindControl("cbStatus")).Attributes.Add("onclick", "javascript:StatusGridDropdown('" + this.Button1.ClientID + "')");

  • Interessante Informationen.

  • Nice Article
    it helped me LOT

  • Great help for me and a very cool code thanks a tone

  • Many many thanks. I also tried by using getElementsByTagName which did not work. I had used the name (given in id field of asp) of the control.

  • Also i have a case where i need to disable two drop downs and one date control (custom control using js). I have disabled the drop downs but in case of the date control the only way to disable it is by setting its ReadOnly property to true. How will i be able to access this property in javascript? This being one problem the other is to reset the value selected (if any) in the drop down to the initial value (on uncheck). Pls tell me if you know the answers to any of these.

  • Good help for me and so many thanks for u people

  • guys help me

    i want to enable and disable file upload control(used for pic upload). when the drop down list index changes(from 0 to 10 ), that many numbar of file control shpuld be enabled..

    0--means no file upload control
    1--means 1 file upload control
    .
    .
    .
    10-means 10 file upload control

  • re: Javascript Enable/Disable Text box using Dropdown

  • Hello
    In my asp.net page .
    i have taken two DropDownList within Gridview.
    i want to disable second dropdownlist on selected index of first dropdownlist.
    please help and give javascript code to do it.

  • Thanks dear Greate Simple piece of code.

  • thanks a lot very useful.

  • I'm using a gridview inwhich every row contains checkbox and textbox when ever i click the check box of a particular row corresponding text box should be enabled.

  • awesome code..thanks a lot my fried..:)

  • how i disabled dropdownbox in javascript

  • Thank you from your Solution.

  • I have a gridview with many rows. Inside this gridview, I have two checkboxes. The first checkbox is enabled on Page_Load. When I check the first one, I need to enable the second checkbox. I also need to disable the second checkbox when the first one is unselected. I am using javascript to insert a time stamp into a textbox. Is there a way to find the 2nd checkbox control (on the same row) so I can enable/ disable it?
    my email is :cmd2112@hotmail.com

    My name is Chris. Thank you.

  • Hi,
    I want to disable one item of DropDownList.

Comments have been disabled for this content.