我有一個Repeater,它有一個GridView,它使用一個帶有刪除命令的SqlDataSource。當我點擊給定行上的刪除時,它會回發,但不會觸發刪除。我忘記了什麼嗎?Repeater中的Gridview不能觸發Delete事件
回答
您是否設置了GridView的DataKeyNames property?它需要設置爲該項目的主鍵來唯一標識要修改的記錄。如果您沒有設置它,單擊刪除(或編輯)將導致回發,因爲DataKey未與每個GridView行關聯,所以回發不會影響任何內容。
按照GridView.DataKeys property頁:
當DataKeyNames屬性是 集,GridView控件 自動創建用於控制每行一個DataKey 對象。 DataKey對象包含在DataKeyNames 屬性中指定的字段或字段 的 值。
例如,假設您要刪除由ProductID字段標識的產品項目。
你的SqlDataSource可能是這個樣子:
<asp:SqlDataSource ID="ProductsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString %>"
SelectCommand= "SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products]"
DeleteCommand="DELETE FROM Products WHERE ProductID = @ProductID">
<DeleteParameters>
<asp:Parameter Name="ProductID" />
</DeleteParameters>
</asp:SqlDataSource>
然後,您可以分配相同的「產品ID」主鍵,把GridView的DataKeyNames屬性:
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
DataKeyNames="ProductID" ...
這些教程可能對你有幫助:
是的,我成功地在我的應用程序中成功使用了這個gridview設置,並且我指定了DataKeyNames。其他幾十個gridviews和這個gridview之間的唯一區別就是它是一個轉發器的孩子。 – 2009-09-21 18:19:55
首先,你應該添加清晰questions.Anyone誰將會盡力幫助您會問你一些代碼一些代碼。
在我看來, *如果您正在使用Button,LinkButton,Image
刪除操作的可能是你忘了使用的CommandName
CommandField中這是在GridView的列聲明:
<asp:CommandField ShowDeleteButton="True" />
模板列可能是:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete" Text="Delete"/>
</ItemTemplate>
</asp:TemplateField>
above declara當點擊鏈接時tions將有相同的結果。檢查您已成功分配CommandName屬性。
*其他意見,如果你看起來更詳細OnRowDeleting情況下,如果你在你使用回傳操作之間的UpdatePanel設置
e.Cancel = true //remove it
*也?
我剛剛設置了一個沒有問題的非常基本的例子。如果沒有發佈代碼(其他許多人已經要求),我們可以幫您解決的問題並不多。
下面是我的示例中的代碼 - 這裏沒有任何棘手,只是將字符串數組綁定到中繼器,並使用指定的select和delete命令將gridview綁定到sqldatasource。
我能想到的是,你的數據源沒有指定delete命令/方法(但你會得到一個明確的錯誤),或者你沒有在gridview中指定的DataKeyNames(但你也會得到一個明確的錯誤)。
<asp:Repeater ID="rp" runat="server">
<ItemTemplate>
<p>Row: <%#Container.DataItem%></p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="CroppingID">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="CroppingID" HeaderText="CroppingID" ReadOnly="True" SortExpression="CroppingID" />
<asp:BoundField DataField="CroppingName" HeaderText="CroppingName" SortExpression="CroppingName" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:Repeater>
和數據源(從我已成立了一個現有的數據庫)
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:sConn %>" DeleteCommand="spD_Cropping"
DeleteCommandType="StoredProcedure" SelectCommand="spS_Croppings"
SelectCommandType="StoredProcedure">
<DeleteParameters>
<asp:Parameter Name="CroppingID" Type="Byte" />
</DeleteParameters>
</asp:SqlDataSource>
- 1. GridView的事件不觸發
- 2. 爲什麼我的Silverlight Datagrid Delete Button不能觸發事件?
- 3. ASP.NET GridView的RowUpdating事件不觸發
- 4. GridView中的DropDownList SelectedIndexChanged不能觸發!
- 5. Repeater中的按鈕不會觸發ItemCommand
- 6. 沒有觸發GridView的OnPageIndexChanging事件ASP.NET
- 7. Click事件不能在HorizBar中觸發
- 8. UpdatePanel中GridView的自定義分頁不會觸發PageIndexChanging事件
- 9. Backbone.js的不能趕上觸發事件
- 10. BackboneJS:REST DELETE不會觸發destroy()
- 11. GridView排序事件未觸發
- 12. 更新事件不是在動態GridView中觸發
- 13. Radiobutton檢查更改事件不在GridView中觸發
- 14. ui-router $ stateChangeState事件不能觸發
- 15. KeyDown事件不能用.NET WinForms觸發?
- 16. ImageButton OnClick事件不能觸發
- 17. JavaScript多個事件不能觸發
- 18. Mouseenter事件未能觸發
- 19. 觸發FLASH事件功能
- 20. 爲什麼我的GridView的SelectedIndexChanged事件不會觸發?
- 21. 嵌套的GridView - 如何觸發Child GridView按鈕Click事件
- 22. 在asp.net Gridview控件中觸發事件的順序是什麼?
- 23. 嵌套的GridView不會觸發行事件
- 24. 我在gridview上的複選框不會觸發事件
- 25. 觸發鍵盤事件,WebKit的鍵盤不會觸發事件
- 26. d3不觸發的事件
- 27. jQuery的事件不觸發
- 28. 的ToolBarButton不觸發事件
- 29. ZKemKeeper:不觸發的事件
- 30. MySQL + delete觸發器
可否請你提供的源代碼中的和平?命令不會觸發的原因有很多。 – Arthur 2009-09-27 09:39:33