我有一个带有复选框和下拉菜单的 GridView 。
默认情况下,该复选框未被选中。 默认情况下,下拉列表处于禁用状态。
在 GridView 的编辑模式下,当用户单击复选框时,我希望启用下拉列表。如果我可以在客户端执行此操作,那就太棒了,如果不能,我想在服务器端执行此操作,而不必单击更新,然后再次编辑。
这是用 C# 编写的
谢谢!
我尝试过的:
grdiview 基于数据源,因此最初我尝试将下拉列表的启用值基于复选框数据值的数据 Eval。然而,这需要选中该框,单击更新,然后编辑才能启用 ddl。然后我想也许自动回发会让所有用户不必单击更新然后再次编辑。这不起作用。然而我真正想要的是一个客户端解决方案。我认为它必须工作的方式是复选框上的事件必须实际启用下拉列表,我不认为下拉列表可以监听要检查的复选框。但是我不知道如何从asp 代码中的另一个控件引用一个控件。所以也许我会说 OnCheckChanged = if Checked then ddl.enabled = true?
但我不知道如何写,而且我不知道我可以强制该事件要在客户端进行评估的复选框。
@Tim - 我尝试过这个:
在 rowdatabound 事件中:
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;");
当我单击“编辑”时,此代码确实会被点击,因此 onclick 事件被添加到复选框中。但是当我单击复选框时,下拉列表不会启用。
谢谢蒂姆!这是有效的解决方案。
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;");
}
最佳答案
使用RowDataBound将客户端事件添加到您的复选框:
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
关于javascript - 基于GridView中的另一个控件动态启用GridView中的ASP.net按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3617729/