How can I find the RepeaterItem I have clicked?
I am creating a simple ASP page that has a Repeater control. This repeater control contains LinkButtons; so that as the repeater increases in item size, the amount of LinkButtons also increases. My question is, how can I identify uniquely the RepeaterItem to which I click the LinkButton?.
I though of using a Foreach to search through the RepeaterItems but I don't know what conditions should I use to identify the Linkbutton I cliked. By the way, each LinkButton have the same name and same ID.
My goal is to get the RepeaterItem in which that Linkbutton belongs
Thanks, 开发者_开发问答Y_Y
- Assign Command/ComandArgument for your LinkButton
- Assign handler 'OnCommand' for your Repeater and create event handler
- In the handler you get RepeaterCommandEventArgs - use item property to access
You can use:
- Tag property of LinkButton to store object to identify the LinkButton
- Included hidden field in Repeater template to store something to identify Linkbutton.
- Using CommandArgument of LinkButton
- ...
You can look at using what is called "ItemCommand"
or you can set the CommandArgument of your LinkButton.
protected void lnkButton_Click(object sender, EventArgs e) {
LinkButton _sender = (LinkButton)sender;
string argument = _sender.CommandArgument;
}
The common solution for this scenario is to use the CommandName and/or CommandArgument properties of the button. Just bind the CommandArgument to the ID of the objects you put into the repeater. The command argument can be accessed in the click handler.
精彩评论