N Spencer
N Spencer

Reputation: 25

ASP C# Repeater Button Not Passing String to Method

I have a repeater that creates buttons from selected items on a checkboxlist. The checkboxlist is a list of item codes.

The buttons appear properly and have the item codes as their text. That works.

On click, the button should call a method using the item code of the button to call a method that populates data to the page, but that is not happening. I believe the button is passing an empty value.

How do I get the repeater button click to pass the correct value? The method works with a regular text box, but I have not been able to get it to work with the repeater buttons.

ASPX

  ` <div style="width: 98%; overflow-x: scroll;">
  <asp:Repeater ID="rptItemButtons"
                runat="server">

            <ItemTemplate>
                <asp:Button ID="btnItemButton"
                    runat="server" 
                    Text='<%# Container.DataItem.ToString() %>'

                     CommandArgument='<%# Container.DataItem.ToString() %>'  
                   CommandName="repeater_ItemCommand"
                     />

            </ItemTemplate>
          </asp:Repeater>
  </div>`

C#

 public void repeater_ItemCommand(object sender, CommandEventArgs e)
    { 
    SaveUserInputsAction();                                                           
    SaveDataAction();
    lblTestMessage.Text = e.CommandArgument.ToString();
     GetItemDetails(e.CommandArgument.ToString());           GetCostFactors(e.CommandArgument.ToString());
    }

Upvotes: 1

Views: 134

Answers (1)

dana
dana

Reputation: 18105

Try setting the OnItemCommand property of the Repeater:

ASPX:

<asp:Repeater ID="rptItemButtons" OnItemCommand="Repeater_ItemCommand" runat="server">
    <ItemTemplate>
        <asp:Button ID="btnItemButton" runat="server"
            Text='<%# Container.DataItem.ToString() %>'
            CommandArgument='<%# Container.DataItem.ToString() %>'
        />
    </ItemTemplate>
</asp:Repeater>

CodeBehind:

public void Repeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{ 
    SaveUserInputsAction();                                                           
    SaveDataAction();
    lblTestMessage.Text = e.CommandArgument.ToString();
    GetItemDetails(e.CommandArgument.ToString());
    GetCostFactors(e.CommandArgument.ToString());
}

EDIT After reviewing your code again, I noticed that you are specifying CommandName="repeater_ItemCommand" on your button. It may actually work if you change that to OnCommand="repeater_ItemCommand". I kind of like specifying the event handler on the Repeater but it may come down to personal preference.

Upvotes: 1

Related Questions