简体   繁体   English

在gridview中单击删除按钮/链接时创建警报时出错

[英]error in creating an alert when delete button/link clicked in the gridview

my goal is to create an alert message when i try to click the delete buttong in gridview. 我的目标是当我尝试在gridview中单击删除buttong时创建警报消息。 I am using asp.net C#. 我正在使用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 编译器错误消息:CS0039:无法通过引用转换,装箱转换,拆箱转换,包装转换或空类型转换将类型'System.Web.UI.WebControls.TableCell'转换为'System.Web.UI.WebControls.ImageButton'

Source Error: 来源错误:

Line 211: // if you are having Links (not images) as the command button. 第211行://如果你有链接(不是图像)作为命令按钮。 Line 212: //LinkButton button = cell as ImageButton; 第212行:// LinkBut​​ton button = cell as ImageButton; Line 213: ImageButton button = control as ImageButton; 第213行: ImageButton按钮=控制为ImageButton; Line 214: if (button != null && button.CommandName == "Delete") Line 215: // Add delete confirmation 第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;";
            }
        }
    }

}

Hi Pedro, Im not familiar in coding using asp.net C# so i have a difficulty in finishing my project. 嗨佩德罗,我不熟悉使用asp.net C#进行编码,所以我在完成项目时遇到了困难。 i am using Visual Studio 2008...Given below: 我正在使用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>     

May i know what should i put in my .aspx.cs file. 我可以知道我应该把什么放在我的.aspx.cs文件中。 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" ? 谢谢佩德罗......我差不多要走了......但还有一个问题......我应该把它放在这里 - >“somethingthatidentifiesRow”? Thanks 谢谢

<asp:LinkButton ID="lnkRemove" runat="server"  CommandArgument="<%# Eval("somethingthatidentifiesRow")%>" 

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. 再次检查错误关于元素的转换,它说你不能将tablecell元素转换为imagebutton元素,所以你正在进行错误的对话,正确找到合适的元素,而不是像我在下面解释的那样。

You need to check given control is ImageButton or not if not than you need to serach out the other control for eampl e 你需要检查给定的控件是否是ImageButton,如果不是你需要搜索另一个控件的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 或者其他方法是通过单元格中的元素的id找到控制而不是松开

ImageButton btn = cell.FindControl("id_of_imagebutton") as ImageButton;
if(btn!=null)
{
  //you code to atttach javascript with button

}

Might be easier to do it from the .aspx 从.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 在.asx.cs

You'll need the following: 你需要以下内容:

public void DeleteFunction(object sender, EventArgs e)
{
    string argumentthatidentifiesRowCell = ((LinkButton)sender).CommandArgument;
    //do your thing to remove
}

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM