如何知道哪些的LinkBut​​ton在ListView被点击 [英] How to know which LinkButton in a ListView was clicked

查看:128
本文介绍了如何知道哪些的LinkBut​​ton在ListView被点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在ListView的ItemTemplate中一个LinkBut​​ton。在ListView每个按钮应该调用相同的单击事件处理程序。然而,在处理,我需要知道哪个按钮被点击。这可能吗?

 < ASP:ListView控件=服务器ID =lvKeyGroup>
    <&LayoutTemplate模板GT;
        <表>
            < ASP:占位符=服务器ID =itemPlaceholder/>
        < /表>
    < / LayoutTemplate模板>
    <&ItemTemplate中GT;
        &所述; TR>
            < TD>< ASP:LinkBut​​ton的=服务器文本=删除的OnClick =lbRemoveAuthGroup_Click/>< / TD>
            < TD><%#的eval(authorizationgroup等)%>< / TD>
        < / TR>
    < / ItemTemplate中>
< / ASP:的ListView>


解决方案

添加的CommandName 属性为每个的LinkBut​​ton 和处理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

这篇关于如何知道哪些的LinkBut​​ton在ListView被点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆