C# 在gridview中单击“删除”按钮/链接时创建警报时出错
我的目标是在尝试单击gridview中的delete按钮时创建一条警报消息。我正在使用asp.net C#。当我尝试运行程序时,遇到以下错误: 编译错误 描述:编译服务此请求所需的资源时出错。请查看以下特定错误详细信息,并适当修改源代码 编译器错误消息:CS0039:无法通过引用转换、装箱转换、取消装箱转换、换行转换或空类型转换将类型“System.Web.UI.WebControl.TableCell”转换为“System.Web.UI.WebControl.ImageButton” 源错误: 第211行://如果您使用链接(而不是图像)作为命令按钮。 第212行://LinkButton button=单元格作为ImageButton; 第213行:ImageButton按钮=控件作为ImageButton强> 第214行:if(button!=null&&button.CommandName==“Delete”) 第215行://添加删除确认C# 在gridview中单击“删除”按钮/链接时创建警报时出错,c#,asp.net,gridview,C#,Asp.net,Gridview,我的目标是在尝试单击gridview中的delete按钮时创建一条警报消息。我正在使用asp.net C#。当我尝试运行程序时,遇到以下错误: 编译错误 描述:编译服务此请求所需的资源时出错。请查看以下特定错误详细信息,并适当修改源代码 编译器错误消息:CS0039:无法通过引用转换、装箱转换、取消装箱转换、换行转换或空类型转换将类型“System.Web.UI.WebControl.TableCell”转换为“System.Web.UI.WebControl.ImageButton” 源错误:
嗨,佩德罗,我不熟悉使用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>
谢谢佩德罗。我马上就要拿到它了。但是还有一个问题。我应该在这里写些什么?>“身份证明行”?谢谢
再次检查关于元素转换的错误,它表示您无法将tablecell元素转换为imagebutton元素,因此您对它进行了错误的对话。请正确找到合适的元素,然后按照下面的说明进行操作
您需要检查给定控件是否为ImageButton,如果不是,则需要为EAMPLE检查另一个控件
foreach (Control control in cell.Controls)
{
if(control is ImageButton)
{
ImageButton button = control as ImageButton;
//you code to atttach javascript with button
}
else
continue;
}
或者,另一种方法是通过单元格中元素的id来查找控件,而不是循环
ImageButton btn = cell.FindControl("id_of_imagebutton") as ImageButton;
if(btn!=null)
{
//you code to atttach javascript with button
}
从.aspx中执行此操作可能更容易
<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>
试试这样的
protected void GridView_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.
ImageButton button = control as ImageButton;
if (button != null && button.CommandName == "Delete") {
// Add delete confirmation
button.OnClientClick = "if (!confirm('Are You Sure to Delete this Vehicle ?')) return;";
}
}
}
}
}
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" OnRowDataBound="GridView_RowDataBound"
AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" CellPadding="3" DataKeyNames="DEVICEID" DataSourceID="SqlDataSource1"
Font-Names="Arial" Font-Size="Smaller" HorizontalAlign="Center" PageSize="50"
Width="100%" EmptyDataText="No Vehicles Found Against the Selected Zone">
<RowStyle ForeColor="#000066" />
<Columns>
<asp:CommandField ShowEditButton=True
DeleteImageUrl="~/Images/del.jpg" DeleteText="Delete Record" ButtonType="Image" CancelImageUrl="~/Images/cancel.png" EditImageUrl="~/Images/edit.png" UpdateImageUrl="~/Images/tick.png">
<ItemStyle Font-Size="8pt" Width="30px" Wrap="False" />
</asp:CommandField>
在网格视图中是这样的
protected void GridView_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.
ImageButton button = control as ImageButton;
if (button != null && button.CommandName == "Delete") {
// Add delete confirmation
button.OnClientClick = "if (!confirm('Are You Sure to Delete this Vehicle ?')) return;";
}
}
}
}
}
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" OnRowDataBound="GridView_RowDataBound"
AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" CellPadding="3" DataKeyNames="DEVICEID" DataSourceID="SqlDataSource1"
Font-Names="Arial" Font-Size="Smaller" HorizontalAlign="Center" PageSize="50"
Width="100%" EmptyDataText="No Vehicles Found Against the Selected Zone">
<RowStyle ForeColor="#000066" />
<Columns>
<asp:CommandField ShowEditButton=True
DeleteImageUrl="~/Images/del.jpg" DeleteText="Delete Record" ButtonType="Image" CancelImageUrl="~/Images/cancel.png" EditImageUrl="~/Images/edit.png" UpdateImageUrl="~/Images/tick.png">
<ItemStyle Font-Size="8pt" Width="30px" Wrap="False" />
</asp:CommandField>
像这样,只有当客户端说“是”并且您有一些东西可以识别行/单元格时,您才能到达“DeleteFunction”!编辑以添加所需的额外信息。请记住,asp:Template字段会进入您希望删除链接的gridview区域。如果您获得了所需信息,请不要将answer标记为Accepted。@macoy更新了关于.aspx.cs GridView1_RowDataBound的我的答案,因为您已单击/onClientClick均已就绪。
protected void GridView_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.
ImageButton button = control as ImageButton;
if (button != null && button.CommandName == "Delete") {
// Add delete confirmation
button.OnClientClick = "if (!confirm('Are You Sure to Delete this Vehicle ?')) return;";
}
}
}
}
}
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" OnRowDataBound="GridView_RowDataBound"
AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" CellPadding="3" DataKeyNames="DEVICEID" DataSourceID="SqlDataSource1"
Font-Names="Arial" Font-Size="Smaller" HorizontalAlign="Center" PageSize="50"
Width="100%" EmptyDataText="No Vehicles Found Against the Selected Zone">
<RowStyle ForeColor="#000066" />
<Columns>
<asp:CommandField ShowEditButton=True
DeleteImageUrl="~/Images/del.jpg" DeleteText="Delete Record" ButtonType="Image" CancelImageUrl="~/Images/cancel.png" EditImageUrl="~/Images/edit.png" UpdateImageUrl="~/Images/tick.png">
<ItemStyle Font-Size="8pt" Width="30px" Wrap="False" />
</asp:CommandField>