c# - 将点击事件附加到 GridView 中的图像上

标签 c# asp.net

我无法将点击事件附加到存储在 GridView 中的图像上。基本上它是一个删除按钮,允许用户根据按钮的位置删除特定的行。我已准备好 C# 中的代码,但是,我似乎无法将点击事件附加到它。

这是按钮的标记代码

  <asp:TemplateField HeaderText="Remove" ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <asp:ImageButton ID="imgbDeleteP" runat="server" BORDER="0" CausesValidation="false" ImageUrl="~/img/Del.png" Height="25px" ImageAlign="Middle"
                               onClick ="gv_Quals_RowCommand" CommandArgument="<%#Container.DataItemIndex%>" CommandName="Remove" />
                        </ItemTemplate>

onClick ="gv_Quals_RowCommand"

点击事件的c#代码

protected void gv_Quals_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if ((e.CommandName == "Remove"))
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = gv_Quals.Rows[index];
            DataTable dtCurrentTable = (DataTable)Session["CurrentTable"];
            dtCurrentTable.Rows[index].Delete();
            if ((dtCurrentTable.Rows.Count < 0))
            {

            }
            else if ((row.Cells[0].Text != "*New*"))
            {
                int appId = 5000;
                //int appId = 1;
                string insProg = ("delete from projectunitassignment where UnitId =" + int.Parse(row.Cells[0].Text));
                SqlCommand cmd = new SqlCommand(insProg, conn);
                cmd.Connection.Close();
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                RebindCat(appId);
            }
        }
    }

这是我不断收到的编译错误

CS0123:“gv_Quals_RowCommand”的重载不匹配委托(delegate)“ImageClickEventHandler”

我无法通过属性设置点击事件,因为它存储在 GridView 中,所以我无法通过那里访问它。此外,点击事件没有运行,因为我已经通过调试进行了测试

最佳答案

问题是 GridViewCommandEventArgs 应该只是 EventArgs

public void imgbDeleteP_Click(object sender, EventArgs e)

编辑:

I see that in your code you use the Command Argument, so if you want to use that you should see this post

Basically use onCommand instead of onClick or cast the sender to button to get the command argument, something like:

var argument = ((ImageButton)sender).CommandArgument;

关于c# - 将点击事件附加到 GridView 中的图像上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50099200/

相关文章:

c# - 如何防止非英文字符并允许非字母字符

c# - AvalonEdit:即使未聚焦也突出显示当前行

c# - ASP.NET session 过期时间

asp.net - WebMethod(有时)返回整个网站

asp.net - asp 按钮图像替代文本不可见

c# - 字符串中不带逗号的单引号替换

c# - 我可以告诉 bindingRedirect 始终使用最新的可用版本吗?

c# - 使用 C# winforms 与 Windows 中的其他桌面应用程序交互

c# - 获取asp.net中pdf文档的当前页码

c# - 尝试在 formview 中绑定(bind)下拉列表时出现空引用异常