8.行命令按钮

  • ListView的行按钮和Repeater一样,不同的是取当前行数据的方式,int index=((ListViewDataItem)e.Item).DisplayIndex取出操作行的行号,ListView1.DataKeys[index].Value取出主键的值,如果对数据进行了操作,最后要对ListView执行DataBind刷新数据,由ListView的DataKeynames属性决定存储哪些字段值为主键,可以多个主键(和数据库主键没有直接关系),所有有Values.
  • 排序:将LayoutTemplate中的表头用<asp:LinkButton runat="server" CommandName="Sort" Text="Id" CommandArgument="Id" />代替,其中CommandArgument的值为排序字段。只要是CommandName、CommandArgument对就可以了,展现成什么,显示在哪都可以。

实例:

   有个数据表有三个字段:id(自增),name(用户名).age(年龄).用listview显示这些数据,并在listview中放一个按钮,每当点击一下这个按钮,这个记录的年龄就会增加一岁。因为我们可以在浏览状态下给其增加一岁,所有把这个行按钮放在itemTemplate中。也放入一按钮用于排序,如下源码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="行命令设置.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
            DeleteMethod="Delete" InsertMethod="Insert" 
            OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" 
            TypeName="行命令设置.DAL.DataSetUserTableAdapters.T_UserTableAdapter" 
            UpdateMethod="Update">
            <DeleteParameters>
                <asp:Parameter Name="Original_id" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="name" Type="String" />
                <asp:Parameter Name="age" Type="Int32" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="name" Type="String" />
                <asp:Parameter Name="age" Type="Int32" />
                <asp:Parameter Name="Original_id" Type="Int32" />
            </UpdateParameters>
        </asp:ObjectDataSource>
        <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1">
            <Fields>
                <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                    ShowNextPageButton="False" ShowPreviousPageButton="False" />
                <asp:NumericPagerField />
                <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" 
                    ShowNextPageButton="False" ShowPreviousPageButton="False" />
            </Fields>
        </asp:DataPager>
        <asp:ListView ID="ListView1" runat="server" DataKeyNames="id" 
            DataSourceID="ObjectDataSource1" InsertItemPosition="None" 
            onitemcommand="ListView1_ItemCommand">
           
           
           
            <EditItemTemplate>
                <tr style="">
                    <td>
                        <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" />
                    </td>
                    <td>
                       
                        <asp:Label ID="idLabel1" runat="server" Text='<%# Eval("id") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="ageTextBox" runat="server" Text='<%# Bind("age") %>' />
                    </td>
                </tr>
            </EditItemTemplate>
            <EmptyDataTemplate>
                <table runat="server" style="">
                    <tr>
                        <td>
                            未返回数据。</td>
                    </tr>
                </table>
            </EmptyDataTemplate>
            <InsertItemTemplate>
                <tr style="">
                    <td>
                        <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清除" />
                    </td>
                    <td>
                        
                        &nbsp;</td>
                    <td>
                        <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="ageTextBox" runat="server" Text='<%# Bind("age") %>' />
                    </td>
                </tr>
            </InsertItemTemplate>
            <ItemTemplate>
                <tr style="">
                    <td>
                         <asp:Button ID="btn" runat="server" Text="排序" CommandName="Sort" CommandArgument="Age" />
                        <asp:Button ID="IncBtn" runat="server" Text="增一岁" CommandName="IncAge" />
                        <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />
                        <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />
                    </td>
                    <td>
                        <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />
                    </td>
                    <td>
                        <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
                    </td>
                    <td>
                        <asp:Label ID="ageLabel" runat="server" Text='<%# Eval("age") %>' />
                    </td>
                </tr>
            </ItemTemplate>
            <LayoutTemplate>
                <table runat="server">
                    <tr runat="server">
                        <td runat="server">
                            <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
                                <tr runat="server" style="">
                                    <th runat="server">
                                    </th>
                                    <th runat="server">
                                        id</th>
                                    <th runat="server">
                                        name</th>
                                    <th runat="server">
                                       
                                        age</th>
                                </tr>
                                <tr runat="server" ID="itemPlaceholder">
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr runat="server">
                        <td runat="server" style="">
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
            
         
            
        </asp:ListView>
    
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    
    </div>
    </form>
</body>
</html>

在listview 的itemCommand中写入以下事件:

  protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "IncAge")
            {
                int i = ((ListViewDataItem)e.Item).DisplayIndex;
               // int id =(int)ListView1.DataKeys[i].Value;
                int id = (int)ListView1.DataKeys[i].Values["id"];
                T_UserTableAdapter adapter = new T_UserTableAdapter();
                adapter.IncAgeById(id);
                ListView1.DataBind();
              

            }
        }

 

posted on 2013-07-03 14:32  天上星  阅读(484)  评论(0编辑  收藏  举报

导航