1. 程式人生 > >GridView的RowCommand事件和DataList的ItemCommand事件

GridView的RowCommand事件和DataList的ItemCommand事件

 

GridView

<asp:GridView ID="GridView1" runat="server"
          CssClass="GridViewStyle" PageSize="10" AllowPaging="true"
             onpageindexchanging="GridView1_PageIndexChanging"
             onrowcommand="GridView1_RowCommand">
          <FooterStyle CssClass="GridViewFooterStyle" />
          <RowStyle CssClass="GridViewRowStyle" />  
          <SelectedRowStyle CssClass="GridViewSelectedRowStyle" />
          <PagerStyle CssClass="GridViewPagerStyle" />
          <AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
          <HeaderStyle  CssClass="GridViewHeaderStyle"/>
          <Columns>
          <asp:BoundField DataField="DRName" HeaderText="飯店名稱" />
          <asp:BoundField DataField="Category" HeaderText="所屬菜系" />
          <asp:BoundField DataField="ConsumptionLevel" HeaderText="人均消費" />
          <asp:BoundField DataField="Remark" HeaderText="折扣" ControlStyle-Width="80px" />
          <asp:BoundField DataField="Telephone" HeaderText="聯絡電話" />
          <asp:TemplateField>
          <ItemTemplate>
           <asp:Button ID="btnDetails" runat="server" Text="詳細" CommandName="GetID"

           CommandArgument='<%#eval_r("ID") %>'/>
          </ItemTemplate>
          </asp:TemplateField>
          </Columns>
         <EmptyDataTemplate>
            提示:當前沒有任何記錄
         </EmptyDataTemplate>
         </asp:GridView>

     protected void GridView1_RowCommand

(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(e.CommandArgument);
                switch (e.CommandName)
                {
                    case "GetID":
                        Server.Transfer("iframeRepeater.aspx?ID=" + id.ToString());
                        break;
                }
            }
            catch
            { }
        }

DataList

<asp:DataList ID="DataList1" runat="server" BorderColor="#CCCCCC" BorderStyle="None"
            BorderWidth="0" CellPadding="3" GridLines="Both" RepeatColumns="3"

            RepeatDirection="Horizontal"
            Width="99%" HorizontalAlign="Center" ItemStyle-BorderWidth="1"
            onitemcommand="DataList1_ItemCommand" >
            <SelectedItemStyle Font-Bold="True" ForeColor="White"

             BackColor="#669999"></SelectedItemStyle>
            <ItemStyle ForeColor="#000066" VerticalAlign="Top" Width="33%"></ItemStyle>
            <ItemTemplate>
                <table cellpadding="1" cellspacing="0" height="220" border="0" width="80%">
                    <tr>
                        <td align="center" style="height: 95px">
                            <a href='Repeater.aspx?num=<%#eval_r("shopNum") %>'>
                                <img src='<%#eval_r("ShopImage") %>' height="250px"

                                width="250px" /></a>
                        </td>
                    </tr>
                    <tr>
                        <td align="center">
                            <a href='Repeater.aspx?num=<%#eval_r("shopNum")%>'>
                                <%#eval_r("shopName")%></a>
                        </td>
                    </tr>
                    <tr>