• gridview控件自定义绑定,响应模板列按钮事件


    ——商家品牌管理

    <asp:GridView ID="GridViewBrand" CellPadding="0" CellSpacing="1" runat="server" AutoGenerateColumns="False" GridLines="None" Width="98%" OnRowDataBound="GridViewBrand_RowDataBound">
                   <Columns>
                       <asp:BoundField DataField="BrandID" HeaderText="序号">
                           <HeaderStyle CssClass="gdv" />
                                    <ItemStyle HorizontalAlign="Center" CssClass="gdvRow2" />
                                </asp:BoundField>
                       <asp:BoundField DataField="BrandName" HeaderText="品牌名">
                           <HeaderStyle CssClass="gdv" />
                                    <ItemStyle HorizontalAlign="Center" CssClass="gdvRow2" />
                                </asp:BoundField>
                                <asp:BoundField DataField="ManufacturerName" HeaderText="生产厂商">
                           <HeaderStyle CssClass="gdv" />
                                    <ItemStyle HorizontalAlign="Center" CssClass="gdvRow2" />
                                </asp:BoundField>                           
                                <asp:TemplateField HeaderText="给商家添加品牌">
                                    <ItemTemplate>
                                    <asp:Button ID="btn_CreateDealerBrand" Text="给商家加入此品牌" DealerID="0" BrandID="0" runat="server" OnClick="CreateDealerBrand"  CssClass="istop1"  OnClientClick="return confirm('给加入品牌?')" />
                                    </ItemTemplate>
                                    <HeaderStyle CssClass="gdv" />
                                    <ItemStyle HorizontalAlign="Center" CssClass="gdvRow2" />
                                </asp:TemplateField>                           
                   </Columns>
               </asp:GridView>

    相应事件后台代码(部分):

    GridViewBrand_RowDataBound事件(在gridview控件的属性事件中可以定义)

      protected void GridViewBrand_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowIndex >= 0)
                {
                    string brandIDStr = e.Row.Cells[0].Text;
                    string brandNameStr =Server.HtmlDecode(e.Row.Cells[1].Text).ToString();
                    Button btn_CreateDealerBrand = (Button)e.Row.Cells[3].FindControl("btn_CreateDealerBrand");
                    btn_CreateDealerBrand.Attributes["BrandID"] = brandIDStr;
                    btn_CreateDealerBrand.Attributes["DealerID"] = dealer.DealerID.ToString();
                    btn_CreateDealerBrand.Text = "给商家[" + dealer.UserName + "]加入品牌[" + brandNameStr + "]";
                    btn_CreateDealerBrand.OnClientClick = "return confirm('给商家[" + dealer.UserName + "]加入品牌[" + brandNameStr + "]?')";
                }
            }

     gridview中的button事件CreateDealerBrand

      protected void CreateDealerBrand(object sender, EventArgs e)
            {
                //权限:系统管理员,总管理员
                if (managerClient.ManagerType != 1 && managerClient.ManagerType != 2)
                {
                    Response.Write("抱歉!请联系管理员。");
                    Response.End();
                }
                Button button = (Button)sender;
                Home.Function.DealersBrand dealerBrand= new Home.Function.DealersBrand();
                dealerBrand.BrandID = int.Parse(button.Attributes["BrandID"].ToString());
                dealerBrand.DealerID = int.Parse(button.Attributes["DealerID"].ToString());
                dealerBrand.IsValid = 1;
                dealerBrand.Create();
                if (dealerBrand.DealerBrandID == 0)
                    AlertMsg = "<script type=\"text/javascript\">alert(\"该商家已经存在此品牌!\");</script>";
                GridViewDatabind();
                GridView_DealerBrandDatabind();
            }

  • 相关阅读:
    Asp.Net MVC3.0中防止跨站的POST
    .NET实现字符串base64编码
    silverlight调用MVC WebApi方法
    MVC实现上传图片的方法
    ASP.NET使用文件上传控件上传图片
    设置网页icon标志
    C# 连接EXCEL 和 ACCESS
    javascript前进、后退、刷新的实现
    DataTable数据统计方法
    常量指针和指针常量
  • 原文地址:https://www.cnblogs.com/yaunion/p/1365686.html
Copyright © 2020-2023  润新知