[英]Enable Disable Checkbox in Asp.net Gridview Based on DropDownList in C#
基於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;
}
}
}
使字符串小寫應該有幫助嗎?
Eval("Reason").ToString().ToLower().Equals("hybrid")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.