在C#中基于DropDownlist在Asp.net Gridview中启用禁用复选框
我的 C# 代码有问题。
我需要在基于 DropDownList 的 asp.net gridview 中启用或禁用复选框。
如果在 DropDownList 中没有选择任何值,则 GridView 中的复选框将被禁用。
当在 DropDownList 中选择了一个值时,GridView 中的复选框将为在 DropDownList 中选择的变量启用。
在 GridView 中,当在 DropDownList 中选择了一个值时,输出是正确的,但复选框始终处于启用状态。
我试过使用这些解决方案但没有成功。
这是我的代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chkSelect = (CheckBox)e.Row.FindControl("chkSelect");
if (DDL.SelectedIndex != 0)
{
chkSelect.Enabled = true;
}
}
}
catch (Exception)
{
}
}
编辑 #1
<asp:DropDownList ID="DDL" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DDL_SelectedIndexChanged"
CssClass="ddl_Class" Enabled="false">
<asp:ListItem Text="-------" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:TemplateField>
<ItemTemplate>
<center>
<asp:CheckBox ID="chkSelect" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged"
AutoPostBack="true" Enabled="false" /></center>
</ItemTemplate>
</asp:TemplateField>
protected void DDL_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkSelect = ((CheckBox)GridView1.Rows[i].FindControl("chkSelect"));
if (DDL.SelectedIndex != 0)
{
chkSelect.Enabled = true;
}
}
GridViewBind();
}
最佳答案
您设置复选框启用或禁用
protected void DDL_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkSelect = ((CheckBox)GridView1.Rows[i].FindControl("chkSelect"));
if (DDL.SelectedIndex != 0)
{
chkSelect.Enabled = true;
}
}
}
关于c# - 基于 C# 中的 DropDownList 在 Asp.net Gridview 中启用禁用复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25441242/