我有一个带有9个按钮图像的checkboxlist控件。 我需要它,如果用户检查复选框项目,将显示相应的按钮图像...检查总数== 3 ..
例如(对于所有图像按钮,包含要检查的权限,可见性将设置为true)
所有这一切都必须在没有任何按钮点击的情况下完成
<asp:CheckBoxList ID="CheckBoxList1" runat="server"
OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged1"
AutoPostBack="True">
<asp:ListItem Value="Privilege 1">Dining</asp:ListItem>
<asp:ListItem Value="Privilege 2">Travel</asp:ListItem>
<asp:ListItem Value="Privilege 3">Shopping</asp:ListItem>
</asp:CheckBoxList>
<table cellspacing="10">
<tr>
<td>
<asp:ImageButton ID="ImageButton1" runat="server" Height="80px"
Width="120px" ImageUrl="imageurl" ToolTip="image" />
</td>
<td>
<asp:ImageButton ID="ImageButton2" runat="server" Height="80px"
Width="120px" ImageUrl="imageurl" ToolTip="image" />
</td>
<td>
<asp:ImageButton ID="ImageButton3" runat="server" Height="80px"
Width="120px" ImageUrl="imageurlg" ToolTip="image" />
</td>
</tr>
</table>
C#背后的代码
protected void CheckBoxList1_SelectedIndexChanged1(object sender, EventArgs e)
{
foreach (ListItem listItem in CheckBoxList1.Items)
{
if (listItem.Selected == true)
{
//Just to check which item is being checked
//However, it only returns one item at a time
Label1.Text = ", " + listItem.Text;
}
}
//list of privileges
string[] privilege = { "Privilege 1", "Privilege 2", "Privilege 3", "Privilege 4", "Privilege 5", "Privilege 6", "Privilege 7" };
ImageButton[] Privilege1 = { ImageButton1, ImageButton5, ImageButton6, ImageButton7, ImageButton8 };
........
if (CheckBoxList1.SelectedValue == privilege[i])
{
//set the visibility of respective button images with this privileges to be true
}
}
如何去做?请帮助...
答案 0 :(得分:0)
一种方法是: 1)默认情况下将所有图像按钮的可见性设置为假(即,由于未选中任何复选框项,因此它们都不可见。)
<asp:ImageButton ID="ImageButton1" runat="server" Height="80px" Width="120px" ImageUrl="imageurl" ToolTip="image" Visible="False"/>
2)在代码隐藏中,确保引用System.Linq。以下是根据选择的项目使图像按钮可见的位置(当然将常数放在更中心的位置):
public void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs args)
{
const string PRIVILAGE1 = "Privilege 1";
const string PRIVILAGE2 = "Privilege 2";
const string PRIVILAGE3 = "Privilege 3";
var checkBoxList = sender as CheckBoxList;
if (checkBoxList == null) return;
var selectedItems = checkBoxList.Items.Cast<ListItem>().Where(x => x.Selected).ToList();
if (!selectedItems.Any()) return;
ImageButton1.Visible = selectedItems.Any(x => x.Value.Equals(PRIVILAGE1, StringComparison.OrdinalIgnoreCase));
ImageButton2.Visible = selectedItems.Any(x => x.Value.Equals(PRIVILAGE2, StringComparison.OrdinalIgnoreCase));
ImageButton3.Visible = selectedItems.Any(x => x.Value.Equals(PRIVILAGE3, StringComparison.OrdinalIgnoreCase));
}