Monday, March 12, 2012

How to handle dynamic control event in repeater?

Hello!

I've met with a problem. I'm building a list of products and use asp.net repeater to display it. each item has to have a LinkButton called "Remove" and I need to handle the click event somehow... :(

Any ideas how to insert a linkbutton into repeater item in a way so I can handle the click event it in the code for all of them?


Put the link button in the repeater

<asp:LinkButton ValidationGroup="Delete" ID="LinkButton1" CommandArgument='<%# Eval("myID") %>">' runat="server" CausesValidation="True"
CommandName="Delete" Text="Delete" OnClick="LinkButton1_Click"></asp:LinkButton>

protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton btn = sender as LinkButton;
String idtoDelete = btn.CommandArgument;
// Run Delete
}

Labels: , , , , , , , , , , , , , ,

How to Handle dynamic Button Click event

Dear sir,

In the above code, I am binding the data such as description of the product, and i have added one button. When I click on the button for the particular row, that row's details has to be added in a separate datatable.


<asp:DataGrid ID="dgGrid" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateColumn HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label1" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' />
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn >
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CommandName="Add To Cart" Text="Add To Cart" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

Please let me know how to proceed.

Thanks in Advance,

Regards,

Arun.

How about something like this:

protected void dgGrid_ItemCommand(object source, DataGridCommandEventArgs e){Button Button1 = e.CommandSourceas Button;if (Button1 ==null) {return; }DataGridItem dgi = Button1.NamingContaineras DataGridItem;if (dgi ==null) {return; }Label Label1 = dgi.FindControl("Label1")as Label;if (Label1 ==null) {return; }string description = Label1.Text;// Do whatever you want here}

Labels: , , , , , , , , , , , ,