2009-08-05 62 views
0

我有一个支持删除的GridView。我想添加一个弹出式窗口,其中有一个问题,如'你确定要删除这一行?'。在删除dataview的行之前确认删除

我的代码:

<asp:GridView id="GridAccounts" runat="server" AutoGenerateColumns="False" 
     ShowFooter="True" DataKeyNames="ID" 
     DataSourceID="AccountDataSource" onrowcommand="GridAccounts_RowCommand"> 
     <SelectedRowStyle BackColor="Lime" /> 
     <Columns> 
      <asp:CommandField ButtonType="Image" ShowDeleteButton="True" DeleteImageUrl="~/Pictures/delete.jpg" /> 
      <asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="ID"> 
       <EditItemTemplate> 
        <asp:Label ID="LabelAccountIDUpdate" runat="server" Text='<%# Eval("ID") %>'></asp:Label> 
       </EditItemTemplate> 
       <FooterTemplate> 
        <asp:Button ID="ButtonAccountIDInsert" runat="server" CommandName="Insert" Text="Insert" /> 
       </FooterTemplate> 
       <ItemTemplate> 
        <asp:Label ID="LabelAccountID" runat="server" Text='<%# Bind("ID") %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 

后面的代码:

protected void GridPaymentMethod_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      ImageButton deleteButton = (ImageButton)e.Row.Cells[0].Controls[0]; 
      MyMoney.PaymentMethodRow row = (MyMoney.PaymentMethodRow)((System.Data.DataRowView)e.Row.DataItem).Row; 
      deleteButton.OnClientClick = string.Format("return confirm('Are you sure you want to delete payment method {0}?');", row.Name.Replace("'", @"\'")); 
     } 
    } 

这为渲染:

<input type="image" src="Pictures/delete.jpg" alt="Delete" onclick="return confirm('Are you sure you want to delete payment method Gotovina?');javascript:__doPostBack('ctl00$MainContent$GridPaymentMethod','Delete$0')" style="border-width:0px;" /> 

如果我点击确认窗口,回发时OK,但没有任何反应。如果我注释掉RowDataBound代码,那么删除工作就OK了。没有确认弹出的代码:

<input type="image" src="Pictures/delete.jpg" alt="Delete" onclick="javascript:__doPostBack('ctl00$MainContent$GridPaymentMethod','Delete$0')" style="border-width:0px;" /> 

我在做什么错了?

回答

1

在你的代码必须更改按钮类型= “图片”,以按钮类型= “链接” - 那么的onclick = “...” 将无JavaScript的渲染:___ doPostBack(...)的一部分。而在GridPaymentMethod_RowDataBound事件中设置类似deleteButton.Text = “< IMG SRC = \” 映像路径\ “.../>”(使用HTML实体,而不是<>)。

或者你可以使用的ImageButton与ComamndName = “删除” 和ConfirmButtonExtender从ASP.NET AjaxToolkit套件。

3

我相信this是你正在尝试做一个例子。它更干净,你不必随背后的代码一起去。

+0

相当。 CommandField没有什么神奇的 - 它只是添加一个标准的LinkBut​​ton,CommandName设置为你想要启动的动作。你可以使用正确的CommandName替换你自己的LinkBut​​ton(例如CommandName =“Delete”会触发删除事件)。 – 2009-08-05 18:07:08

+0

我用这个教程:http://www.asp.net/Learn/data-access/tutorial-22-cs.aspx上的CommandName – sventevit 2009-08-06 06:12:54

+0

大尖而不ASPX onclick处理程序。 – adrianos 2009-11-25 10:14:41

0
deleteButton.OnClientClick = string.Format("((!confirm('Are you sure you want to delete payment method {0}?'))?return false);", row.Name.Replace("'", @"\'"));