1

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());
    }
1
  • I think you need to use Container.ItemIndex as a CommandArgument instead of Container.DataItem Commented Apr 7, 2017 at 19:52

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That solved it! Thanks @dana, you are the best, I really appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.