黑马程序员 ListView获取将被删除的行的主键

来源:互联网 发布:p2p网络借贷系统 编辑:程序博客网 时间:2024/05/18 18:17

------- Windows Phone 7手机开发、.Net培训、期待与您交流! -------


ASP.NET中的ListView控件,可以很方便的实现数据的显示、编辑、更新、新增、删除等操作。

    我实现的功能是这样的:

   1、 数据源取出所有的角色(字段:Id、Name),用<%#Eval(“Id”)%>绑定字段。

    2、额外放个DropDownLIst控件ddlRoleList,在ListView的ItemDataBound事件中,根据角色Id,从数据库中取出该角色的权限,并加载到ddlRoleList中。

    3、在每行的末尾显示“删除”按钮。

      数据显示没问题,但是发现无法删除数据行,在ItemDeleting事件中也无法获取选择行的主键Id。

aspx代码如下:

<asp:ObjectDataSource ID="odsRoleList" runat="server" DeleteMethod="Delete" SelectMethod="ListAll" TypeName="TMCR.BLL.RoleBLL">        <DeleteParameters>            <asp:Parameter Name="Id" Type="Int32" />        </DeleteParameters>    </asp:ObjectDataSource>    <asp:ListView ID="lvRoleList" runat="server" DataSourceID="odsRoleList"         onitemdatabound="lvRoleList_ItemDataBound" DataKeyNames="ID"         onitemdeleting="lvRoleList_ItemDeleting">        <EmptyDataTemplate>            ……        </EmptyDataTemplate>        <ItemTemplate>            <tr >                <td >                    <%# Eval("ID") %>                </td>                <td >                    <%# Eval("Name") %>                </td>                <td >                    <asp:DropDownList ID="ddlRoleRights" CssClass="ddlStyle" runat="server"></asp:DropDownList>                </td>                <td>                    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />                </td>            </tr>        </ItemTemplate>        <LayoutTemplate>            <table runat="server">                <tr runat="server" >                    <td runat="server">                        <table ID="itemPlaceholderContainer" runat="server" >                                <tr >                                <td >ID</td>                                <td >角色名称</td>                                <td">角色拥有的权限[权限名 | 权限URL | 模块名  ]</td>                                    <td >操作</td>                                </tr>                                <tr ID="itemPlaceholder" runat="server">                            </tr>                        </table>                    </td>                </tr>                <tr runat="server">                    <td runat="server" style="">                        <asp:DataPager ID="DataPager1" runat="server">                            <Fields>                                <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"                                     ShowLastPageButton="True" />                            </Fields>                        </asp:DataPager>                    </td>                </tr>            </table>        </LayoutTemplate>    </asp:ListView>

         Cs代码如下:


protected void lvRoleList_ItemDataBound(object sender, ListViewItemEventArgs e)        {            if (e.Item.ItemType == ListViewItemType.DataItem )            {                TMCR.BLL.RightBLL bll = new TMCR.BLL.RightBLL();                //取得当前行的角色id                object obj = e.Item.DataItem;                if (obj == null)                {                    return;                }                int roleID = ((TMCR.Model.Role)obj).ID;                //取得当前行的角色的权限信息                DataTable dt = bll.GetRightsByRole(roleID);                //取得DropDownList对象                DropDownList ddlRoleRights = (DropDownList)e.Item.FindControl("ddlRoleRights");                //加载角色的权限信息                foreach (DataRow row in dt.Rows)                {                    ddlRoleRights.Items.Add(row[1].ToString() + " | " + row[2].ToString() + " | " + row[3].ToString());                }            }        } 

         原因:未指定ListView.DataKeyNames

         MSDN关于ListView.DataKeyNames的解释:

“获取或设置一个数组,该数组包含了显示在ListView 控件中的项的主键字段的名称。

使用 DataKeyNames 属性指定表示数据源主键的字段。 若要以声明方式设置此属性,请使用以逗号分隔的字段名列表。

设置 DataKeyNames 属性后,ListView 控件自动用指定字段的值填充其 DataKeys 集合。这提供了一个访问各项的主键的便捷方法。

为了使 ListView 控件的自动更新和删除功能工作,必须设置 DataKeyNames 属性。 这些键字段的值将传递到数据源控件,以便匹配要更新或删除的项。

在更新和删除操作期间,通过使用DataKeyNames 属性指定为数据键的字段将使用 Keys 集合传递到数据源控件。即使数据键字段没有绑定到模板中的控件,它们也将传递到数据源控件。”

解决:如果要实现删除操作,必须要指定DataKeyNames属性。

Ps.

在ListView.ItemDeleting事件中得到该行的主键值:

protected void lvRoleList_ItemDeleting(object sender, ListViewDeleteEventArgs e)        {            object key = lvRoleList.DataKeys[e.ItemIndex].Value;        }

------- Windows Phone 7手机开发、.Net培训、期待与您交流! -------
原创粉丝点击