views:

135

answers:

2

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;");
}
+1  A: 

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

Chuck
I updated my question. Sorry, should have included that info from the start.
kralco626
+1  A: 

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
Tim Schmelter
@tim - cool. I'll translate that to C# and try it out. Although could I also do something like onclick = document.getElementById("MyDropdownlistID.ClientID").enabled = this.checked;") right in the asp.net code?
kralco626
@kralco626: could be that the clientid is not yet set in RowCreated-Event. But perhaps you can use RowDataBound instead(think this attribute is saved in ViewState, isnt it?). There the ClientID-Property should be final.
Tim Schmelter
@Tim - ya let me try that. I was just about to post saying it did not work that could be why
kralco626
@Tim - I updated my questions with what I tried. It does not work.
kralco626
@kralco: sorry, you didnt mentioned my updated answer. I changed "... = this.checked;" into "= ! this.checked;". The important is the exclamation mark what means "not". On checking the checkbox the drowdown will be enabled(not disabled) now.
Tim Schmelter
@Tim - I saw your change before, but I used enabled rather than disabled. I didn't even think there was a property called disabled. I thought it was enabled...?
kralco626
@Tim - But i guess i'm wrong, the client side property must be called diabled? Becuase it worked. I'll post my final code in my questions if you wanna add the C# solution to your answer.
kralco626
The serverside property is Enabled(http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.enabled.aspx) and means that the control isn't even rendered as html. The clientside attribute is disabled: http://www.codetoad.com/javascript/enable_disable_form_element.asp
Tim Schmelter
@Tim - actually you have to set Visible=false for the control not to be rendered. Enabled=false will render the control but with the client side property "disabled" set to true.
kralco626
@kralco: Of course, i mixed it up ;)Glad that i could help.
Tim Schmelter
@Tim - haha, it's cool. Thanks so much for the help!
kralco626