Dynamically enable ASP.net button in GridView based on another control in the GridView
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, GridViewRowE开发者_如何学PythonventArgs 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;");
}
Use RowDataBound to add the client-side event to your checkbox:
GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Select Case e.Row.RowState
Case DataControlRowState.Edit
Dim chk As CheckBox = DirectCast(e.Row.FindControl("MyCheckboxID"), CheckBox)
Dim ddl As DropDownList = DirectCast(e.Row.FindControl("MyDropdownlistID"), DropDownList)
chk.Attributes.Add("onclick", "document.getElementById('" & ddl.ClientID & "').disabled = ! this.checked;")
End Select
End Sub
What have you tried so far? This should be easily accomplished with a TemplateField and a little javascript http://msdn.microsoft.com/en-us/library/ms228046.aspx
精彩评论