2009-08-05 61 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("'", @"\'"));