基于复选框列表项的按钮图像可见性

发布于 2025-01-02 03:55:24 字数 2106 浏览 3 评论 0原文

我有一个带有 9 个按钮图像的复选框列表控件。 我需要它,如果用户选中复选框项目,将显示相应的按钮图像...检查总数 == 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 
        }
}

如何去做呢?请帮忙....

I have a checkboxlist control with 9 button images.
I need it such that if user check on the checkbox item, respective button images will be shown... Total number of checks == 3..

for e.g. (for all images button tat contains the privileges being checked, visibility will be set to true)

ALL THIS MUST BE DONE WITHOUT ANY BUTTON CLICKS

<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>

Code behind 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 
        }
}

How to go about doing it?? pls helppp....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北斗星光 2025-01-09 03:55:24

一种方法是:
1) 默认情况下将所有图像按钮的可见性设置为 false(即,由于没有选择复选框项目,它们一开始都是不可见的)。

<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));
    }

One way is:
1) Set all the Image button's visibility to false by default (i.e. they all start off invisible as no check-box item is selected).

<asp:ImageButton ID="ImageButton1" runat="server" Height="80px" Width="120px"    ImageUrl="imageurl" ToolTip="image" Visible="False"/>

2) In the code-behind make sure you reference System.Linq. The following does the bit of making the image buttons visible depending on which items are selected (place the constants in a more central spot of course):

 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));
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文