Error generating alert when delete / link button is clicked in gridview
My goal is to create a warning message when I try to click the delete icon in the gridview. I am using asp.net C #. when i try to run my program i am facing this error:
Compilation Error Description: An error occurred while compiling a resource required to service this request. Please review the specific error details below and modify your source code accordingly.
Compiler Error Message: CS0039: Unable to convert type "System.Web.UI.WebControls.TableCell" to "System.Web.UI.WebControls.ImageButton" via referential conversion, boxing conversion, unboxing conversion, boxing conversion, or null type conversion
Source error:
Line 211: // if there are links (not images) as a command button. Line 212: // LinkButton = cell as ImageButton; Line 213: ImageButton = control as ImageButton; Line 214: if (button! = Null & button.CommandName == "Delete") Line 215: // Add confirmation confirmation
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;";
}
}
}
}
Hi Pedro I am not familiar with coding using asp.net C # so I am having a hard time completing my project. I am using Visual Studio 2008 ... Below:
<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>
May I know what should I put in the .aspx.cs file. Thanks you
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
Thanks Pedro .. almost on my way to get it ... but one more question ... what should I put here -> something that identifies Row? Thanks to
<asp:LinkButton ID="lnkRemove" runat="server" CommandArgument="<%# Eval("somethingthatidentifiesRow")%>"
source to share
Check again for an error in the element transformation. It says that you cannot convert a tablecell element to an imagebutton element so that you do the wrong talk, so that it finds the right element correctly and than does it as described below.
You need to check if the given control is an ImageButton or not, if not, then you need to infect another control with eampl e
foreach (Control control in cell.Controls)
{
if(control is ImageButton)
{
ImageButton button = control as ImageButton;
//you code to atttach javascript with button
}
else
continue;
}
or another way is to search for the control using the id of the element in your cell, not an anchor
ImageButton btn = cell.FindControl("id_of_imagebutton") as ImageButton;
if(btn!=null)
{
//you code to atttach javascript with button
}
source to share
It might be easier to do it from .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>
Include .asx.cs
You will need the following:
public void DeleteFunction(object sender, EventArgs e)
{
string argumentthatidentifiesRowCell = ((LinkButton)sender).CommandArgument;
//do your thing to remove
}
source to share
Try something like this
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;";
}
}
}
}
}
And in grid view something like this
<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>
source to share