2014-02-20 63 views
1

這有幾個問題。現在,我所要做的就是讓delete命令起作用。編輯也不工作,猜測問題是相似的。這是我自己的一個學習項目。任何幫助是極大的讚賞。謝謝。ASP.NET SqlDataSource Gridview問題

<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 

在此先感謝。

回答

3

您的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" /> 
+0

這似乎沒有做到這一點。我收到的錯誤是:「過程或函數'sproc_del_MenuItem'期望參數'@MenuID',它沒有提供。」 – user2891487

+0

檢查我的更新。將DataKeyNames屬性添加到GridView –

1

你是用戶自動生成列設置爲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> 

現在會得到你的菜單ID後面。 希望這個作品