单击参数时在代码块中循环

发布于 2024-08-21 04:36:58 字数 635 浏览 3 评论 0原文

基本上我想做的是显示类别列表。如果管理员已登录 我想在每个类别旁边显示一些按钮。例如删除按钮。问题是我不知道如何将参数传递给执行该操作的函数。

就像我指定在单击按钮时必须调用函数“DeleteCat”,但如果我无法传递要删除的类别的 ID,则这将不起作用。

我知道这可以通过命令和中继器来完成,但这不是一个选项,我不能使用中继器。

显然这就是我的目标:

但当然这是行不通的。

    <%For Each Cat In Category.Children%>
        <p class="SubCategory">
            <%=Cat.Name%> 

            <%If User.Identity.Name = "Admin" Then%>
                <asp:LinkButton ID="LinkButton6" runat="server" OnClick="AddItem" Text="A+" CommandArgument=<%=Cat.ID %> />
            <%End If%>
        </p>
    <%Next %>

Basically what i am trying to do is display a list of categories. And if the admin is logged in
i want to show some buttons next to each category. For example a button to delete it. The problem is that i dont know how to pass a parameter to the function that does the action.

Like i specify that on button click the function 'DeleteCat' must be called but if i cant pass the ID of the category to be deleted this wont work.

I know this can be done with commands and a repeater, but its not an option, i cant use a repeater.

So apparanly this is what i am aiming for:

But of course it does not work.

    <%For Each Cat In Category.Children%>
        <p class="SubCategory">
            <%=Cat.Name%> 

            <%If User.Identity.Name = "Admin" Then%>
                <asp:LinkButton ID="LinkButton6" runat="server" OnClick="AddItem" Text="A+" CommandArgument=<%=Cat.ID %> />
            <%End If%>
        </p>
    <%Next %>

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

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

发布评论

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

评论(1

梦回旧景 2024-08-28 04:36:58

您的事件处理程序 AddItem 应该能够评估事件的发送者CommandEventArgs

void AddItem(Object sender, EventArgs e)
{
    int result;
    bool returnValue;

    Button clickedButton = (Button)sender;
    returnValue = Int32.TryParse(clickedButton.CommandArgument, result);

    // then other stuff happens ...
}

在此处查看详细示例

编辑

按照布兰登的建议完成了代码示例。


替代解决方案,使用 CheckBoxList

    ...
    <script language="C#" runat="server">
        void Page_Load(Object Sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // bind data to controls
                this.itemRepeater.DataSource = Category.Children;
                this.adminList.DataSource = Category.Children;
                this.itemRepeater.DataBind();
                this.adminList.DataBind();
            }

            // set visibility according to user
            this.itemRepeater.Visible = (User.Identity.Name != "Admin");
            this.adminList.Visible = (User.Identity.Name == "Admin");
            this.adminButton.Visible = (User.Identity.Name == "Admin");
        }

        protected void AddItem(object sender, EventArgs e)
        {
            foreach(ListItem currentitem in this.adminList.Items)
            {
                if(currentitem.Selected)
                {
                    // do something with the selected value
                    int i;
                    bool isparsed = Int32.TryParse(currentitem.value, i);
                }
            }
        }
    </script>
</head>
<body>
    <form id="mainForm" runat="server">
        <asp:Repeater ID="itemRepeater" runat="server">
            <ItemTemplate>
                <p><%# DataBinder.Eval(Container.DataItem, "value") %></p>
            </ItemTemplate>
        </asp:Repeater>
        <asp:CheckBoxList ID="adminList" runat="server" />
        <br />
        <asp:Button ID="adminButton" OnClick="AddItem" runat="server" Text="Add seleted Items" />
    </form>
</body>

Your event handler AddItem should be able to evaluate the sender of the event and the CommandEventArgs

void AddItem(Object sender, EventArgs e)
{
    int result;
    bool returnValue;

    Button clickedButton = (Button)sender;
    returnValue = Int32.TryParse(clickedButton.CommandArgument, result);

    // then other stuff happens ...
}

See detailed example here.

edit

Completed code sample as suggested by Brandon.


Alternative solution, using a CheckBoxList

    ...
    <script language="C#" runat="server">
        void Page_Load(Object Sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // bind data to controls
                this.itemRepeater.DataSource = Category.Children;
                this.adminList.DataSource = Category.Children;
                this.itemRepeater.DataBind();
                this.adminList.DataBind();
            }

            // set visibility according to user
            this.itemRepeater.Visible = (User.Identity.Name != "Admin");
            this.adminList.Visible = (User.Identity.Name == "Admin");
            this.adminButton.Visible = (User.Identity.Name == "Admin");
        }

        protected void AddItem(object sender, EventArgs e)
        {
            foreach(ListItem currentitem in this.adminList.Items)
            {
                if(currentitem.Selected)
                {
                    // do something with the selected value
                    int i;
                    bool isparsed = Int32.TryParse(currentitem.value, i);
                }
            }
        }
    </script>
</head>
<body>
    <form id="mainForm" runat="server">
        <asp:Repeater ID="itemRepeater" runat="server">
            <ItemTemplate>
                <p><%# DataBinder.Eval(Container.DataItem, "value") %></p>
            </ItemTemplate>
        </asp:Repeater>
        <asp:CheckBoxList ID="adminList" runat="server" />
        <br />
        <asp:Button ID="adminButton" OnClick="AddItem" runat="server" Text="Add seleted Items" />
    </form>
</body>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文