我在dropdownlist
中有一个checkbox
和一个RadGrid
。
我最初设置了下拉列Enabled="false"
。因此,在页面加载时,下拉列表将被禁用,现在,如果我选中RadGrid中的任何复选框,则应启用下拉列表。
复选框:
<telerik:GridTemplateColumn UniqueName="CheckBoxTemplateColumn">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="ToggleRowSelection"
AutoPostBack="True" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="headerChkbox" runat="server" OnCheckedChanged="ToggleSelectedState"
AutoPostBack="True" />
</HeaderTemplate>
</telerik:GridTemplateColumn>
DROPDOWNLIST:
<telerik:RadDropDownList ID="ddlAction" Enabled="false" DefaultMessage="Action" AutoPostBack="true" DataValueField="action" OnSelectedIndexChanged="ddlAction_SelectedIndexChanged" runat="server">
<Items>
<telerik:DropDownListItem Text="" Value="" />
<telerik:DropDownListItem Text="Refresh Mail Status" Value="Refresh Mail Status" />
<telerik:DropDownListItem Text="Send All" Value="Send All" />
<telerik:DropDownListItem Text="Send Selected" Value="Send Selected"/>
<telerik:DropDownListItem Text="Remove" Value="Remove" />
</Items>
</telerik:RadDropDownList>
代码背后:
protected void ddlAction_SelectedIndexChanged(object sender, DropDownListEventArgs e)
{
CheckBox chk = (CheckBox)sender;
chk.FindControl("CheckBox1");
if (!chk.Checked) {
ddlAction.Enabled = true;
}
}
ToggleRowSelection
protected void ToggleRowSelection(object sender, EventArgs e)
{
((sender as CheckBox).NamingContainer as GridItem).Selected = (sender as CheckBox).Checked;
bool checkHeader = true;
foreach (GridDataItem dataItem in radgridCCBList.MasterTableView.Items)
{
if (!(dataItem.FindControl("CheckBox1") as CheckBox).Checked)
{
checkHeader = false;
break;
}
}
GridHeaderItem headerItem = radgridCCBList.MasterTableView.GetItems(GridItemType.Header)[0] as GridHeaderItem;
(headerItem.FindControl("headerChkbox") as CheckBox).Checked = checkHeader;
}
ToggleSelectedState
protected void ToggleSelectedState(object sender, EventArgs e)
{
CheckBox headerCheckBox = (sender as CheckBox);
foreach (GridDataItem dataItem in radgridCCBList.MasterTableView.Items)
{
(dataItem.FindControl("CheckBox1") as CheckBox).Checked = headerCheckBox.Checked;
dataItem.Selected = headerCheckBox.Checked;
}
}
此处,ToggleRowSelection
和ToggleSelectedState
方法是,当我选中该特定行的复选框时,选择特定的网格行。
答案 0 :(得分:2)
请尝试使用以下代码段。
方法一:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlAction.Enabled = false;
}
}
protected void ToggleRowSelection(object sender, EventArgs e)
{
ValidateCheckbox();
}
protected void ValidateCheckbox()
{
bool IsEnabled = false;
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
CheckBox CheckBox1 = item["CheckBoxTemplateColumn"].FindControl("CheckBox1") as CheckBox;
if (CheckBox1.Checked)
{
IsEnabled = true;
break;
}
}
ddlAction.Enabled = IsEnabled;
}
方法2:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlAction.Enabled = false;
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
ValidateCheckbox();
}
protected void ValidateCheckbox()
{
bool IsEnabled = false;
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
CheckBox CheckBox1 = item["CheckBoxTemplateColumn"].FindControl("CheckBox1") as CheckBox;
if (CheckBox1.Checked)
{
IsEnabled = true;
break;
}
}
ddlAction.Enabled = IsEnabled;
}
更新1:
protected void ValidateCheckbox()
{
bool IsEnabled = false;
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
CheckBox CheckBox1 = item["CheckBoxTemplateColumn"].FindControl("CheckBox1") as CheckBox;
if (CheckBox1.Checked)
{
IsEnabled = true;
break;
}
}
GridHeaderItem headerItem = radgridCCBList.MasterTableView.GetItems(GridItemType.Header)[0] as GridHeaderItem;
if ((headerItem.FindControl("headerChkbox") as CheckBox).Checked)
{
IsEnabled = true;
}
ddlAction.Enabled = IsEnabled;
}
更新2 :(仅禁用RadDropDownList中的项目)
<强> ASPX 强>
<telerik:RadDropDownList ID="ddlAction" DefaultMessage="Action" AutoPostBack="true" DataValueField="action" runat="server">
<Items>
<telerik:DropDownListItem Text="" Value="" />
<telerik:DropDownListItem Text="Refresh Mail Status" Value="Refresh Mail Status" />
<telerik:DropDownListItem Text="Send All" Value="Send All" />
<telerik:DropDownListItem Text="Send Selected" Value="Send Selected" />
<telerik:DropDownListItem Text="Remove" Value="Remove" />
</Items>
</telerik:RadDropDownList>
<强> ASPX.CS 强>
protected void Page_Load(object sender, EventArgs e)
{
// To disable "Send All" option in DropDown
DisableItemInDropDown("Send All");
// To disable "Remove" option in DropDown
DisableItemInDropDown("Remove");
}
protected void DisableItemInDropDown(string ddlItemValueText)
{
foreach (DropDownListItem item in ddlAction.Items)
{
if (item.Value == ddlItemValueText)
{
item.Enabled = false;
}
}
}