使用单击事件在gridview运行时中添加按钮

时间:2014-04-12 19:48:39

标签: c# asp.net gridview

我想要做的就是在gridview中添加一个按钮,我想用它来执行当前行数据的一些功能

这是我的gridview代码

<asp:GridView ID="dgvHMDEditorialManage" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="dgvHMDEditorialManage_RowCommand" OnRowCreated="dgvHMDEditorialManage_RowCreated" >
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

                    <EditRowStyle BackColor="#999999" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />

                </asp:GridView>  

C#代码

 using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString()))
        {
            SqlCommand cmd = new SqlCommand(query, con);
            con.Open();
            DataSet ds = new DataSet();

            dgvHMDEditorialManage.DataSource = cmd.ExecuteReader();
            dgvHMDEditorialManage.DataBind();

        }  

这里我正在设置gridview的数据源 现在我想在gridview中添加一个按钮,在点击它时我想用当前行数据执行一些功能
任何人都可以建议我如何做到这一点 感谢

1 个答案:

答案 0 :(得分:0)

你也可以这样做

    <asp:GridView ID="dgvHMDEditorialManage" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false">
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

                    <EditRowStyle BackColor="#999999" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                    <Columns>
                    <asp:TemplateField>
       <ItemTemplate>
            <asp:Button ID="Buttonid" runat="server" CommandName="Item" Text="Test" OnClick="abc_onclick" ></asp:Button>    
            </ItemTemplate>
        </asp:TemplateField>
                    <asp:TemplateField>
        <ItemTemplate>
        <asp:Label ID="mm" runat="server" Text='<%# Bind("Menu") %>'></asp:Label>
        </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField>
        <ItemTemplate>
        <asp:Label ID="mm1" runat="server" Text='<%# Bind("ParentID") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField><asp:TemplateField>
        <ItemTemplate>
        <asp:Label ID="mm2" runat="server" Text='<%# Bind("MenuID") %>'></asp:Label>
        </ItemTemplate>
 </asp:TemplateField>
 </Columns>
                </asp:GridView> 
代码背后的

 protected void abc_onclick(object sender, EventArgs e)
        {
            Button btnEdit = (Button)sender;

            GridViewRow grdrow = (GridViewRow)((Button)sender).NamingContainer;
            Label rowNumber = (Label)dgvHMDEditorialManage.Rows[grdrow.RowIndex].Cells[0].FindControl("mm");
            Label rowNumber1 = (Label)dgvHMDEditorialManage.Rows[grdrow.RowIndex].Cells[1].FindControl("mm1");
            Label rowNumber2 = (Label)dgvHMDEditorialManage.Rows[grdrow.RowIndex].Cells[2].FindControl("mm2");

            string abc = rowNumber.Text;
            string dealId = rowNumber1.Text;
            string dealTitle = rowNumber2.Text; ;
            //do something
        }

和页面加载代码

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            loadGrid();
            }
        }
        public void loadGrid()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Menu", typeof(string));
            dt.Columns.Add("ParentID", typeof(string));
            dt.Columns.Add("MenuID", typeof(string));


            dt.Rows.Add("Name", "1", "1");
            dt.Rows.Add("Name1", "1", "2");
            dt.Rows.Add("Name2", "1", "3");
            dt.Rows.Add("Name3", "1", "4");
            dt.Rows.Add("Name4", "1", "5");
            dt.Rows.Add("Class", "2", "6");
            dt.Rows.Add("Class1", "2", "7");
            dt.Rows.Add("Class2", "2", "8");
            dt.Rows.Add("Class3", "2", "9");
            dt.Rows.Add("Class4", "2", "10");
            dgvHMDEditorialManage.DataSource = dt;
            dgvHMDEditorialManage.DataBind();

        }