我有一個支持刪除的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;" />
我在做什麼錯了?
相當。 CommandField沒有什麼神奇的 - 它只是添加一個標準的LinkButton,CommandName設置爲你想要啓動的動作。你可以使用正確的CommandName替換你自己的LinkButton(例如CommandName =「Delete」會觸發刪除事件)。 – 2009-08-05 18:07:08
我用這個教程:http://www.asp.net/Learn/data-access/tutorial-22-cs.aspx上的CommandName – sventevit 2009-08-06 06:12:54
大尖而不ASPX onclick處理程序。 – adrianos 2009-11-25 10:14:41