日期:2014-05-17  浏览次数:20820 次

用GridViewDeleteEventArgs代替EventArgs报错的问题
gridview每一行里都有一个按钮,点击后的事件

        protected void shanchu_Click(object sender, GridViewDeleteEventArgs e)
        {
            if (int.Parse(GridView1.DataKeys[e.RowIndex][1].ToString()) < 1)
            {
                if (sqlhelper.ExecuteNonQuery("delete from dingdan where id='" + int.Parse(GridView1.DataKeys[e.RowIndex][1].ToString()) + "' and username='" + sessionok.returnsession("userid") + "'") == 1)
                {
                    getdata(ViewState["sql"].ToString());
                }
            }
        }

用GridViewDeleteEventArgs是为了获取当前行的参数,也就是为了获取e.RowIndex,但是会报错误
编译器错误消息: CS0123: “shanchu_Click”的重载均与委托“System.EventHandler”不匹配
只能使用默认的protected void shanchu_Click(object sender, EventArgs e),但是怎么获取当前行的参数呢,也就是怎么获取RowIndex

------解决方案--------------------
部分代码给你参考下:
html:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            onrowcommand="GridView1_RowCommand">
            <Columns>
                <asp:TemplateField HeaderText="序号">
                    <ItemTemplate>
                        <asp:Button ID="Button1" runat="server" Text="Button"  CommandName="btn" CommandArgument='<%#Eval("id") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


cs代码:
 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Button bt = e.CommandSource as Button;
            string id = bt.CommandArgument;//绑定的数据
            switch (e.CommandName)
            {
          &