本文介绍了如何检查转发器中按下了哪个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以用vb代码告诉我如何获得我在转发器中按下的按钮的ID
因为btnclick事件将会不支持,因为转发器中的每个行都有按钮。
Can anyone tell in vb code how can i get the id of button which i pressed in repeater
because btnclick event will not support because button is present in repeater against each row.
推荐答案
<asp:Repeater ID="myRepeater" runat="server" OnItemCommand="myRepeater_ItemCommand" OnItemDataBound="myRepeater_ItemDataBound">
<ItemTemplate>
<asp:Button ID="ButtonDelete" Text="Delete" runat="server"/>
</ItemTemplate>
</asp:Repeater>
public class MyData
{
public int ID { get; set; }
public string Name { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<MyData> data = new List<MyData>();
data.Add(new MyData { ID = 1, Name = "Item 1" });
data.Add(new MyData { ID = 5, Name = "Item 2" });
data.Add(new MyData { ID = 123, Name = "Item 3" });
myRepeater.DataSource = data;
myRepeater.DataBind();
}
}
protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// Note this event is configured on the repeater in the markup
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
// get the data
MyData data = (MyData)e.Item.DataItem;
// get the button
Button ButtonDelete = (Button)e.Item.FindControl("ButtonDelete");
// set the command name and argument
ButtonDelete.CommandName = "Delete";
ButtonDelete.CommandArgument = data.ID.ToString();
}
}
protected void myRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
// Note this event is configured on the repeater in the markup
switch(e.CommandName)
{
case "Delete":
// get the ID of the button clicked from the argumenmt
int id = int.Parse(e.CommandArgument.ToString());
// delete the data that matches this id
break;
}
}
这篇关于如何检查转发器中按下了哪个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!