![](/img/trans.png)
[英]How to refresh gridview when I use sqldatasource in asp.net
[英]ASP.NET SqlDataSource Gridview Problems
這有一些問題。 現在,我要做的就是讓delete命令起作用。 編輯也不起作用,並且猜測問題是相似的。 這是我自己的學習項目。 任何幫助是極大的贊賞。 謝謝。
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="Both" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" >
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
<Columns>
<asp:BoundField DataField="MenuID" HeaderText="ID" Visible="true" ReadOnly="true" />
<asp:TemplateField HeaderText="Date">
<EditItemTemplate>
<asp:TextBox ID="txtGridDate" runat="server" Text='<%# Bind("Date", "{0:M/dd/yyyy}")%>' Width="75px"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblGridDate" runat="server" Text='<%# Bind("Date", "{0:M/dd/yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<EditItemTemplate>
<asp:TextBox ID="txtGridDescription" runat="server" Text='<%# Bind("Description") %>' Width="600px"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblGridDescription" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MealType">
<EditItemTemplate>
<asp:DropDownList ID="ddlGridMealType" runat="server">
<asp:ListItem Value="1" Text="Breakfast"></asp:ListItem>
<asp:ListItem Value="2" Text="Dinner"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblGridType" runat="server" Text='<%# Bind("MealType") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowEditButton="true" ValidationGroup="edit" />
<asp:CommandField ButtonType="Button" ShowDeleteButton="true" />
</Columns>
</asp:GridView>
<asp:SqlDataSource
ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CampRandolphConnectionString %>"
SelectCommand="sproc_sel_AllMeals" SelectCommandType="StoredProcedure"
InsertCommand="sproc_ins_Meal" InsertCommandType="StoredProcedure"
UpdateCommand="sproc_edit_Meal" UpdateCommandType="StoredProcedure"
DeleteCommand="sproc_del_MenuItem" DeleteCommandType="StoredProcedure" >
<InsertParameters>
<asp:Parameter DbType="Date" Name="Date" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="MealCategoryID" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="MenuID" Type="Int32" />
<asp:Parameter DbType="Date" Name="Date" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="MealCategoryID" Type="Int32" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="MenuID" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
這是存儲過程:
ALTER PROCEDURE [dbo].[sproc_del_MenuItem]
@MenuID int
AS
BEGIN
DELETE FROM CampRandolph.dbo.Menu
WHERE @MenuID = MenuID
END
提前致謝。
您的Where子句在您的存儲過程中是錯誤的。 應該:
ALTER PROCEDURE [dbo].[sproc_del_MenuItem]
@MenuID int
AS
BEGIN
DELETE FROM CampRandolph.dbo.Menu
WHERE MenuID = @MenuID
END
在GridView的定義中,添加DataKeyNames屬性:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="MenuID" ....
我還讀到HeaderText必須與鍵名相同:
<asp:BoundField DataField="MenuID" HeaderText="MenuID" Visible="true" ReadOnly="true" />
您是用戶自動生成的列設置為false,並且您還使用了
<asp:BoundField DataField="MenuID" HeaderText="ID" Visible="true" ReadOnly="true" />
而不是使用:
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox id="menuidtxt" runat="server" Text='<%# Eval("MenuID")%>'/>
</ItemTemplate>
</asp:TemplateField>
現在將恢復您的menuid。 希望這有效
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.