New Hand New Hand - 3 months ago 23
C# Question

C# Repeater dynamic Button

I am using repeater to generate the button control.

<asp:Repeater ID="rptDoc" runat="server">
<ItemTemplate>
<div style="...">
<asp:Button ID="btnUpload" runat="server" Text="Button" OnClick="button_Click" />
</div>
</ItemTemplate>
</asp:Repeater>


For example, I can generate 4 buttons. However, how can I implement
the program inside each of the button?

when btnUpload1 clicked, it will upload files into Folder1,
when btnUpload2 clicked, it will upload files into Folder2

I know there is a way that create a Table in the web form, and then
dynamically put the generated button inside the table cells... But I
have lots of DIVs with styling, so I don't want to use a table to
place the dynamic buttons.

Thanks for help.

Answer

It works for me now, I used the LinkButton instead of Button. Also added OnItemCommand="ItemCommand" in repeater. And finally it will goto ItemCommand event when LinkButton clicked. It gets the value from CommandArgument which set in dynamic LinkButton

<asp:Repeater ID="rpt" runat="server" OnItemCommand="ItemCommand">
<ItemTemplate>
<asp:LinkButton runat="server" CommandArgument='<%# Eval("lbtnUploadCommandArgument")%>' CommandName="ButtonEvent">Upload Files</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>


protected void ItemCommand(Object Sender, RepeaterCommandEventArgs e)
{
string[] arr_Para = ((LinkButton)e.CommandSource).CommandArgument.Split(';');
}
Comments