【发布时间】:2012-04-03 13:31:41
【问题描述】:
我的目标是在我尝试单击 gridview 中的删除按钮时创建一条警报消息。我正在使用 asp.net C#。当我尝试运行我的程序时,我遇到了这个错误:
编译错误 说明:在编译服务此请求所需的资源期间发生错误。请查看以下特定错误详细信息并适当修改您的源代码。
编译器错误消息:CS0039:无法通过引用转换、装箱转换、拆箱转换、包装转换或空类型转换
来源错误:
第 211 行://如果您将链接(不是图像)作为命令按钮。 Line 212: //LinkButton button = cell as ImageButton; 第 213 行:ImageButton button = control as ImageButton; 第 214 行:if (button != null && button.CommandName == "Delete") 第 215 行:// 添加删除确认
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// loop all data rows
foreach (DataControlFieldCell cell in e.Row.Cells)
{
// check all cells in one row
foreach (Control control in cell.Controls)
{
// Must use LinkButton here instead of ImageButton
// if you are having Links (not images) as the command button.
//LinkButton button = cell as ImageButton;
ImageButton button = control as ImageButton;
if (button != null && button.CommandName == "Delete")
// Add delete confirmation
button.OnClientClick = "if (!confirm('Are you sure " +
"you want to delete this record?')) return;";
}
}
}
}
嗨 Pedro,我不熟悉使用 asp.net C# 进行编码,所以我在完成我的项目时遇到了困难。我正在使用 Visual Studio 2008...如下所示:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server" CommandArgument="<%# Eval("somethingthatidentifiesRow")%>"
OnClientClick="return confirm('Do you want to delete?')" Text="Delete"
OnClick="DeleteFunction">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
我可以知道我应该在我的 .aspx.cs 文件中添加什么吗?谢谢
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
谢谢佩德罗..我几乎在我的路上得到它......但还有 1 个问题..我应该在这里放什么 --> “somethingthatidentifiesRow”?谢谢
<asp:LinkButton ID="lnkRemove" runat="server" CommandArgument="<%# Eval("somethingthatidentifiesRow")%>"
【问题讨论】:
-
如果您获得了您想要的信息,请不要将答案标记为已接受 ..
-
@macoy 更新了我关于 .aspx.cs GridView1_RowDataBound 的回答,因为您已经完成了 onClick/onClientClick。