Reputation: 31
my goal is to create an alert message when i try to click the delete buttong in gridview. I am using asp.net C#. when i try to run my program, i am encountering this error:
Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0039: Cannot convert type 'System.Web.UI.WebControls.TableCell' to 'System.Web.UI.WebControls.ImageButton' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
Source Error:
Line 211: // if you are having Links (not images) as the command button. Line 212: //LinkButton button = cell as ImageButton; Line 213: ImageButton button = control as ImageButton; Line 214: if (button != null && button.CommandName == "Delete") Line 215: // Add delete 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, Im not familiar in coding using asp.net C# so i have a difficulty in finishing my project. i am using Visual Studio 2008...Given 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 my .aspx.cs file. Thank you
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
Thanks Pedro..im almost on my way to get it...but 1 more question..what should i put here --> "somethingthatidentifiesRow" ? Thanks
<asp:LinkButton ID="lnkRemove" runat="server" CommandArgument="<%# Eval("somethingthatidentifiesRow")%>"
Upvotes: 3
Views: 1693
Reputation: 1294
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>
Upvotes: 0
Reputation: 649
Might be easier to do it from the .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>
On .asx.cs
You'll need the following:
public void DeleteFunction(object sender, EventArgs e)
{
string argumentthatidentifiesRowCell = ((LinkButton)sender).CommandArgument;
//do your thing to remove
}
Upvotes: 1
Reputation: 176934
Check the error again its about the conversion of element, It says that you cannot convert tablecell element to imagebutton element so you are doing wrong conversation to it properly find proper element and than do it asI explained below.
You need to check given control is ImageButton or not if not than you need to serach out the other control for 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 other way is to do is find control by the id of the element in your cell rather than loopeing
ImageButton btn = cell.FindControl("id_of_imagebutton") as ImageButton;
if(btn!=null)
{
//you code to atttach javascript with button
}
Upvotes: 1