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>
                        <td align="center">
                            <%#eval_r("shopPhone")%>
                            <asp:Button ID="btnDetails" runat="server" Text="详细"
                            CommandName="showDetails" CommandArgument='<%#Eval
                         ("shopNum") %>'/>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList>
 
 protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            try
            {
                string num = e.CommandArgument.ToString();
                switch (e.CommandName)
                {
                    case "showDetails":
                        Server.Transfer("Repeater.aspx?num=" + num);
                        break;
                }
            }
            catch { }
        }
eg: 

(1)aspx:

                            <asp:DataList ID="dlServiceItem" runat="server" Width="100%">
                                <ItemTemplate>
                                    <div class="Items_ContentLine" style="text-align:left;"> 
                                        <div class="Items_ContentLineLeft">
                                            <a href='WxServiceItem_View.aspx?Id=<%# Eval("ItemId")%>' title="查看明细" target="_blank" class="ItemsA"><%# Eval("ItemName")%></a>
                                        </div>
                                        <div class="Items_ContentLineRight">
                                            <asp:HyperLink ID="hlFlow" runat="server" Text="123" NavigateUrl="#" CssClass="serviceItem_Button" Width="63px" Height="38px"></asp:HyperLink>
                                            <asp:HyperLink ID="hlAttach" runat="server" Text="123" NavigateUrl="#" CssClass="serviceItem_Button" Width="63px" Height="38px"></asp:HyperLink>
                                            <asp:Button ID="btnProcess" runat="server" Text="123" CssClass="serviceItem_Button" Width="63px" Height="38px" BorderWidth="0" BackColor="Transparent" CommandName="showProcess" CommandArgument='<%#Eval("ItemId") %>' />
                                        </div>
                                    </div>
                                </ItemTemplate>
                            </asp:DataList>
(2)cs:

        void dlServiceItem_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ServiceItem objItem = (ServiceItem)e.Item.DataItem;

            HyperLink hlAttach = (HyperLink)e.Item.FindControl("hlAttach");
            HyperLink hlFlow = (HyperLink)e.Item.FindControl("hlFlow"); 
            Button btnProcess = (Button)e.Item.FindControl("btnProcess");

            hlAttach.Enabled = false;
            hlFlow.Enabled = false;
            btnProcess.Enabled = false; 

            if (objItem.IsAttach > 0)
            {
                hlAttach.Enabled = true;
            }
            if (objItem.IsFlow > 0)
            {
                hlFlow.Enabled = true;
            }
            if (objItem.ApplyMode != (int)EnumServiceItemApplyMode.None)
            {
                btnProcess.Enabled = true; 
            }

            if (hlAttach.Enabled)
            {
                hlAttach.Attributes.Add("onmouseover", "btnMouseover(this);");
                hlAttach.Attributes.Add("onmouseout", "btnMouseout(this);");
                hlAttach.Attributes.Add("onclick", string.Format("attachDownload('{0}'); return false;", objItem.ItemId));
            }
            else
            {
                hlAttach.CssClass = "serviceItem_ButtonDisable";
            }

            if (hlFlow.Enabled)
            {
                hlFlow.Attributes.Add("onmouseover", "btnMouseover(this);");
                hlFlow.Attributes.Add("onmouseout", "btnMouseout(this);");
                hlFlow.Attributes.Add("onclick", string.Format("flowView('{0}'); return false;", objItem.ItemId));
            }
            else
            {
                hlFlow.CssClass = "serviceItem_ButtonDisable";
            }

            if (btnProcess.Enabled)
            {
                btnProcess.Attributes.Add("onmouseover", "btnMouseover(this);");
                btnProcess.Attributes.Add("onmouseout", "btnMouseout(this);");
            }
            else
            {
                btnProcess.CssClass = "serviceItem_ButtonDisable";
            } 
        } 

        void dlServiceItem_ItemCommand(object source, DataListCommandEventArgs e)
        {
            try
            {
                string strItemId = e.CommandArgument.ToString();
                if (e.CommandName=="showProcess")
                {
                    ServiceItem objItem = BaseBLLFactory.CreateService<ServiceItemService>().GetServiceItem(strItemId);
                    if (objUser == null)
                    {
                        alertLogin.Attributes.Add("style", "display:block;"); 
                    }
                    else
                    {
                        string itemUrl = objItem.ApplyUrl;

                        if (objItem.ApplyMode == (int)EnumServiceItemApplyMode.EnterprisePlat)
                        {
                            if (string.IsNullOrEmpty(objItem.ApplyUrl))
                            {
                                itemUrl = "/psp/Index.aspx";
                            }
                        } 
                        //Response.Write("<script>window.open('" + itemUrl + "','_blank')</script>");//原窗口保留,另外新增一个新页面;
                       Response.Redirect(itemUrl);
                        //Server.Transfer(itemUrl);
                    } 
                } 
            }
            catch (Exception err)
            {
                ShowMessage(err.Message);
            }
        }       



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值