【问题标题】:error in creating an alert when delete button/link clicked in the gridview在gridview中单击删除按钮/链接时创建警报时出错
【发布时间】:2012-04-03 13:31:41
【问题描述】:

我的目标是在我尝试单击 gridview 中的删除按钮时创建一条警报消息。我正在使用 asp.net C#。当我尝试运行我的程序时,我遇到了这个错误:

编译错误 说明:在编译服务此请求所需的资源期间发生错误。请查看以下特定错误详细信息并适当修改您的源代码。

编译器错误消息:CS0039:无法通过引用转换、装箱转换、拆箱转换、包装转换或空类型转换

来源错误:

第 211 行://如果您将链接(不是图像)作为命令按钮。 Line 212: //LinkBut​​ton 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。

标签: c# asp.net gridview


【解决方案1】:

再次检查有关元素转换的错误,它表示您无法将 tablecell 元素转换为 imagebutton 元素,因此您对其进行了错误的对话,正确地找到了正确的元素,而不是按照我下面的解释进行操作。

您需要检查给定控件是否为 ImageButton,如果不是,则需要为 empl 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;
}

或其他方法是通过单元格中元素的 id 找到控件,而不是循环

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

}

【讨论】:

    【解决方案2】:

    从 .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>
    

    在 .asx.cs 上

    您需要以下物品:

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

    【讨论】:

    • 这样,如果客户说是,并且您有一些东西可以识别行/单元格,那么您只能到达“DeleteFunction”!
    • 已编辑以添加请求的额外信息。
    • 请记住,asp:templatefield 会进入您想要删除链接的 gridview 区域。
    【解决方案3】:

    试试这样的

    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>
    

    【讨论】:

      猜你喜欢
      • 2016-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-21
      • 1970-01-01
      相关资源
      最近更新 更多