我无法将点击事件附加到存储在 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 ofonClick
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/