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();
        }

原文地址:https://www.cnblogs.com/yaunion/p/1365686.html