Search code examples
c#asp.netitemdatabound

No overload for 'ItemDataBound' matches delegate 'System.EventHandler'


I have a nested repeater Because of that no control in my code is visible and because of that I found out that I had to use FindControl to get the controls visible. I want to pass on a value from a lable after a buttonclick but I'm having a hard time getting it right

The label with the value

<asp:Label ID="lblordernr" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.sid").ToString()%>'></asp:Label>

The button I use:

<asp:Button ID="btnPaid" runat="server" Text="Paid" OnClick="rlrtAdres_ItemDataBound"
                        Style="height: 26px" CssClass="knop" CommandName="btnPaid" />

RepeaterItemDataBound

protected void rlrtAdres_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    Label lblordernr = e.Item.FindControl("lblordernr") as Label;
    Repeater myRepeater = (Repeater)sender;
    Button btn = (Button)sender;
    switch (btn.CommandName)
    {
        case "btnPaid":
            MutateSold("paid", lblordernr.Text);
            break;
    }
}

I first had a normal buttonOclick control but I just dont know how to combine it with the label value and call the final method "MutateSold" for database purposes with a nested repeater and every invisible control in this repeater

protected void btnButton_Click(object sender, EventArgs e)
{
    Control myControllblordernr = FindControl("lblordernr");
    Button btn = (Button)sender;
    switch (btn.CommandName)
    {
        case "btnPaid":
            MutateSold("paid", myControllblordernr.ToString());
            break;
     }
}

Solution

  • You're getting the error you posted in the title because the Click event of btnPaid expects an event handler with a signature like:

    protected void btnButton_Click(object sender, EventArgs e)

    You're pointing it at a method that has a signature of:

    protected void rlrtAdres_ItemDataBound(object sender, RepeaterItemEventArgs e)

    If btnPaid and lblordernr are in the same ItemTemplate for the repeater, you can call FindControl on the button to get a reference to lblordernr. For example, if btnPaid is declared like this:

    <asp:Button ID="btnPaid" runat="server" Text="Paid" OnClick="btnPaid_Click"
        Style="height: 26px" CssClass="knop"  />
    

    the following code will get you a reference to lblOrderNr.

    protected void btnPaid_Click(object sender, EventArgs e)
    {
        var btnPaid = (Button)sender;
        var lblOrderNr = (Label)btnPaid.FindControl("lblordernr");
        var labelText = lblOrderNr.Text;
    
        //Do whatever else needs to be done
    }
    

    Alternatively, you can just add the value in lblOrderNr to the CommandArgument property on the button and get it right from there as well.

    <asp:Button ID="btnPaid" runat="server" Text="Paid" OnClick="btnPaid_Click"
        CommandArgument='<%# DataBinder.Eval(Container, "DataItem.sid").ToString()%>'
        Style="height: 26px" CssClass="knop" />
    
    
    protected void btnPaid_Click(object sender, EventArgs e)
    {
        var btnPaid = (Button)sender;
        var labelText = btnPaid.CommandArgument;
    
        //Do whatever else needs to be done
    }