2014-01-22 141 views
1

我有一個gridview,我添加了一個ItemTemplate來顯示每一行的複選框。我試圖簡單地刪除在文件後面的代碼中檢查的行。我的問題是,我的代碼後面沒有檢測到複選框。這是非常奇怪的,因爲我覺得我沒有改變任何東西,但代碼不再有效(它在兩天前運行,但本地保存這些更改 - 沒有上傳到TFS)。從GridView刪除選中的行

GridView控件(編輯,包括裝訂):

<%--Data Grid--%> 
<asp:GridView ID="Grid_Recipe" CssClass="gridMain" runat="server" OnSelectedIndexChanged="Grid_Recipe_SelectedIndexChanged" AutoGenerateColumns="False" DataKeyNames="Recipe_ID" DataSourceID="DataSource_Grid_Unfiltered" EnableViewState="True" AllowPaging="True" AllowSorting="True" BorderStyle="Solid" BorderWidth="1px" CellPadding="1" CellSpacing="1" HorizontalAlign="Center"> 

    <AlternatingRowStyle BackColor="#CCFFFF" /> 

    <Columns> 
     <asp:CommandField ShowSelectButton="True" SelectText="Edit" /> 
     <asp:TemplateField HeaderText="Select"> 
      <ItemTemplate> 
       <asp:CheckBox ID="deleteCheckbox" runat="server" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:BoundField DataField="Recipe_ID" HeaderText="Recipe_ID" InsertVisible="False" ReadOnly="True" SortExpression="Recipe_ID" Visible="false" /> 
     <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> 
     <asp:BoundField DataField="Difficulty" HeaderText="Difficulty" SortExpression="Difficulty" /> 
     <asp:BoundField DataField="Meal" HeaderText="Meal" SortExpression="Meal" /> 
     <asp:BoundField DataField="Cook_Time" HeaderText="Cook Time" SortExpression="Cook_Time" /> 
     <asp:BoundField DataField="Directions" HeaderText="Directions" SortExpression="Directions" /> 
    </Columns> 
    <HeaderStyle BackColor="#0096D6" ForeColor="White" HorizontalAlign="Right" /> 
</asp:GridView> 

<%--Delete Button--%> 
<asp:Button runat="server" ID="deleteButton" Text="Delete Checked" OnClick="DeleteRows" /> 


<%--Data Sources--%> 
<asp:SqlDataSource ID="Meal_Filter" runat="server" ConnectionString="<%$ ConnectionStrings:LYNNAU_ConnectionString %>" SelectCommand="SELECT DISTINCT [Meal] FROM [Recipe]"></asp:SqlDataSource> 
<asp:SqlDataSource ID="DataSource_Grid_Unfiltered" runat="server" ConnectionString="<%$ ConnectionStrings:LYNNAU_ConnectionString %>" SelectCommand="SELECT * FROM [Recipe]" UpdateCommand="UPDATE [Recipe] SET [Name] = @Name, [Difficulty] = @Difficulty, [Meal] = @Meal, [Cook_Time] = @Cook_Time, [Directions] = @Directions WHERE [Recipe_ID] = @Recipe_ID"> 
    <DeleteParameters> 
     <asp:Parameter Name="Recipe_ID" Type="Int32" /> 
    </DeleteParameters> 
    <UpdateParameters> 
     <asp:Parameter Name="Name" Type="String" /> 
     <asp:Parameter Name="Difficulty" Type="String" /> 
     <asp:Parameter Name="Meal" Type="String" /> 
     <asp:Parameter Name="Cook_Time" Type="Int32" /> 
     <asp:Parameter Name="Directions" Type="String" /> 
     <asp:Parameter Name="Recipe_ID" Type="Int32" /> 
    </UpdateParameters> 
</asp:SqlDataSource> 
<asp:SqlDataSource ID="DataSource_Grid_Meal" runat="server" ConnectionString="<%$ ConnectionStrings:LYNNAU_ConnectionString %>" SelectCommand="SELECT * FROM [Recipe] WHERE ([Meal] = @Meal)" UpdateCommand="UPDATE [Recipe] SET [Name] = @Name, [Difficulty] = @Difficulty, [Meal] = @Meal, [Cook_Time] = @Cook_Time, [Directions] = @Directions WHERE [Recipe_ID] = @Recipe_ID"> 
    <DeleteParameters> 
     <asp:Parameter Name="Recipe_ID" Type="Int32" /> 
    </DeleteParameters> 
    <SelectParameters> 
     <asp:ControlParameter ControlID="Meal_DDL" Name="Meal" PropertyName="SelectedValue" Type="String" /> 
    </SelectParameters> 
    <UpdateParameters> 
     <asp:Parameter Name="Name" Type="String" /> 
     <asp:Parameter Name="Difficulty" Type="String" /> 
     <asp:Parameter Name="Meal" Type="String" /> 
     <asp:Parameter Name="Cook_Time" Type="Int32" /> 
     <asp:Parameter Name="Directions" Type="String" /> 
     <asp:Parameter Name="Recipe_ID" Type="Int32" /> 
    </UpdateParameters> 
</asp:SqlDataSource> 

代碼背後:

[WebMethod] 
protected void DeleteRows(object sender, EventArgs e) 
{ 
    dbCRUD delete = new dbCRUD(); 
    foreach(GridViewRow grd in Grid_Recipe.Rows) 
    { 
     if(grd.RowType == DataControlRowType.DataRow) 
     { 
      if((grd.FindControl("deleteCheckbox") as CheckBox).Checked) 
      { 
       string id = Grid_Recipe.DataKeys[grd.RowIndex].Value.ToString(); 
       //int intID = Convert.ToInt32(Grid_Recipe.SelectedDataKey.Value); 
       int intID = int.Parse(id); 
       if(delete.DeleteRecord(intID) == 1) 
       { 
        resultsDelete.Text = "SQL Exception"; 
        resultsDelete.Visible = true; 
        break; 
       } 
       else if(delete.DeleteRecord(intID) == 2) 
       { 
        resultsDelete.Text = "Non SQL Exception"; 
        resultsDelete.Visible = true; 
        break; 
       } 
       else 
       { 
        resultsDelete.Text = "Record(s) deleted"; 
        resultsDelete.Visible = true; 
       } 
      } 
     } 
    } 
} 

任何事情在我的代碼,將防止被檢測的複選框?我在該方法上放了一個斷點,然後到達它檢查複選框驗證的位置,但是在遍歷每一行後,沒有檢測到行。提前致謝!

+0

因此,所有的複選框被發現,但沒有被選中,對不對?在這種情況下,你可以顯示網格視圖綁定的代碼,最好是整個頁面事件處理程序發生這種情況? – Andrei

+0

剛剛添加了源代碼控件。我試圖將數據綁定到網格,如果頁面回發,因爲我的想法是數據庫已經更新,但這可能是錯誤的。 –

回答

1

它在我看來像服務器端,它不會知道任何關於您的GridView,因爲你有EnableViewState="False"設置。

將其設置爲true並查看是否有幫助。如果沒有檢查如何綁定GridView,並確保您沒有將其重新綁定到Page_Load中的null值或DeleteRows函數運行之前的值。發佈你如何綁定,因爲這將有助於診斷問題。

我最近也發佈了一個回答類似的問題,你可以在這裏找到: iterate through gridview rows on button click

+0

將EnableViewState更改爲true,但沒有效果。我曾將編輯中的數據源控件發佈到我的原始問題中。 –

+0

我刪除了PostBack網格綁定,它似乎解決了我的問題。我在刪除記錄後但在我的DeleteRows方法中添加了網格綁定,並且該功能正常工作。談到另一個問題,但感謝您的幫助! –