如何知道哪些的LinkButton在ListView被点击 [英] How to know which LinkButton in a ListView was clicked
问题描述
我目前在ListView的ItemTemplate中一个LinkButton。在ListView每个按钮应该调用相同的单击事件处理程序。然而,在处理,我需要知道哪个按钮被点击。这可能吗?
< ASP:ListView控件=服务器ID =lvKeyGroup>
<&LayoutTemplate模板GT;
<表>
< ASP:占位符=服务器ID =itemPlaceholder/>
< /表>
< / LayoutTemplate模板>
<&ItemTemplate中GT;
&所述; TR>
< TD>< ASP:LinkButton的=服务器文本=删除的OnClick =lbRemoveAuthGroup_Click/>< / TD>
< TD><%#的eval(authorizationgroup等)%>< / TD>
< / TR>
< / ItemTemplate中>
< / ASP:的ListView>
添加的CommandName
属性为每个的LinkButton
和处理ListView的 ItemCommand
事件。
此外,你需要设置ListView的 DataKeys
属性为您的数据源对象唯一标识符名称。在你可以得到选定的行datakey:
无效ListView1_ItemCommand(对象发件人,ListViewCommandEventArgs E)
{
//在假设数据项的唯一标识符类型的Int32
VAR dataKey =(int)的ListView1.DataKeys [e.Item.DataItemIndex] .value的; 开关(e.CommandName)
{
案删除:
//你的code在这里
打破;
}
}
请按照此链接的ListView控件概述: http://msdn.microsoft。 COM / EN-US /库/ bb398790.aspx
此外,观看此视频:<一href=\"http://www.pluralsight-training.net/microsoft/players/PSODPlayer?author=dan-wahlin&name=webforms-03&mode=live&clip=0&course=aspdotnet-webforms4-intro\" rel=\"nofollow\">http://www.pluralsight-training.net/microsoft/players/PSODPlayer?author=dan-wahlin&name=webforms-03&mode=live&clip=0&course=aspdotnet-webforms4-intro
I currently have a LinkButton in the ItemTemplate of a ListView. Each button in the ListView should call the same click event handler. However, in the handler I need to know which button was clicked. Is this possible?
<asp:ListView runat="server" ID="lvKeyGroup">
<LayoutTemplate>
<table>
<asp:Placeholder runat="server" ID="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>[<asp:LinkButton runat="server" Text="Remove" OnClick="lbRemoveAuthGroup_Click" />]</td>
<td><%# Eval("AuthorizationGroup") %></td>
</tr>
</ItemTemplate>
</asp:ListView>
Add CommandName
property to each LinkButton
and handle ListView's ItemCommand
event.
Also you need to set ListView's DataKeys
property to your datasource object unique identifier name. The you can get selected row datakey:
void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
// in assumption that your data item's unique identifier type is Int32
var dataKey = (int)ListView1.DataKeys[e.Item.DataItemIndex].Value;
switch(e.CommandName)
{
case "Remove":
// your code here
break;
}
}
Follow this link for ListView control overview: http://msdn.microsoft.com/en-us/library/bb398790.aspx
Also, watch this video: http://www.pluralsight-training.net/microsoft/players/PSODPlayer?author=dan-wahlin&name=webforms-03&mode=live&clip=0&course=aspdotnet-webforms4-intro
这篇关于如何知道哪些的LinkButton在ListView被点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!