views:

65

answers:

1

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 %>
A: 

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>
Filburt
To be a little more specific, the next line (where "other stuff happens") will read something like Int32 catId = clickedButton.CommandArgument; - that will give you the ID of the item clicked.
Brandon Montgomery
@Brandon: Added code for evaluating CommandArgument as you suggested.
Filburt