I have a gridview with a cheackbox and a dropdown.
The checkbox, by default, is not checked. The dropdownlist, by default, is disabled.
In the edit mode of the gridview, when the user clicks the checkbox I want the dropdown to become enabled. If I could do this client side that would be awesome, if not I want to do it server side WITHOUT having to click update and then edit again.
This is in C#
Thanks!
What I tried:
The grdiview is based off of a data source, so originally I tried basing the enabled value of the dropdown list off of the data Eval of the checkbox datavalue. However this required checking the box, clicking update and then edit for the ddl to be enabled. Then I thought maybe autopostback would all the user not to have to click update and then edit again. This did not work. However what I really want it a client side solution. I think the way it would have to work is and event on the checkbox would have to actually enable the dropdown list, I don't think the dropdown list can listen for the checkbox to be checked. However I don't know how to reference a control from another control in asp code. So maybe I would say something like OnCheckChanged = if Checked then ddl.enabled = true?
But i'm not sure how to write that, and I don't know that I can force that event of the checkbox to be evaluated client side.
@Tim - I tried this:
in the rowdatabound event:
CheckBox chk = e.Row.FindControl("checkbox1") as CheckBox;
DropDownList ddl = e.Row.FindControl("dropdownlist1") as DropDownList;
chk.Attributes.Add("onclick", "document.getElementById('" + ddl.ClientID + "').enabled = this.checked;");
When I click edit this code DOES get hit so the onclick event IS getting added tot he checkbox. But when I click the checkbox the dropdown list does not become enabled.
Thanks Tim! This is the working solution.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)\
{
if ((row.RowType == DataControlRowType.DataRow) && ((row.RowState & DataControlRowState.Edit) > 0))
CheckBox chk = e.Row.FindControl("checkbox1") as CheckBox;
DropDownList ddl = e.Row.FindControl("dropdownlist1") as DropDownList;
chk.Attributes.Add("onclick", "document.getElementById('" + ddl.ClientID + "').disabled = !this.checked;");
}