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

时间:2012-04-03 13:31:42

标签: c# asp.net gridview

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

编译错误 描述:编译服务此请求所需的资源时发生错误。请查看以下特定错误详细信息并相应地修改源代码。

编译器错误消息:CS0039:无法通过引用转换,装箱转换,拆箱转换,换行或转换,将类型'System.Web.UI.WebControls.TableCell'转换为'System.Web.UI.WebControls.ImageButton' null类型转换

来源错误:

第211行://如果你有链接(不是图像)作为命令按钮。 第212行:// LinkBut​​ton button = cell as ImageButton; 第213行: ImageButton按钮=控制为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;";
            }
        }
    }

}

嗨佩德罗,我不熟悉使用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)
{ 


}

感谢佩德罗......几乎在我的路上得到它...但还有一个问题......我应该把它放在这里 - &gt; “somethingthatidentifiesRow”?感谢

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

3 个答案:

答案 0 :(得分:1)

再次检查错误关于元素的转换,它说你无法将tablecell元素转换为imagebutton元素,所以你正在做错误的对话,正确找到合适的元素,而不是像我在下面解释的那样。

你需要检查给定的控制是否是ImageButton,如果不是你需要搜索另一个控件来进行实施

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

}

答案 1 :(得分:1)

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

您需要以下内容:

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

答案 2 :(得分:0)

尝试这样的事情

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>