从GridView中删除已检查的行

时间:2014-01-22 14:50:25

标签: c# asp.net gridview checkbox

我有一个gridview,我添加了一个ItemTemplate来显示每行的复选框。我试图简单地删除代码隐藏文件中检查的行。我的问题是我的代码后面没有检测到复选框。这很奇怪,因为我觉得我没有改变任何东西,但代码不再有效(它在两天前工作,但在本地保存了这些更改 - 没有上传到TFS)。

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;
                }
            }
        }
    }
}

我的代码中有什么东西阻止检测到复选框?我在方法上放了一个断点,然后我到达了检查复选框验证的位置,但是在迭代遍历每一行之后,没有检测到任何行。提前谢谢!

1 个答案:

答案 0 :(得分:1)

在我看来,就像服务器端一样,它对GridView没有任何了解,因为你设置了EnableViewState="False"

将其设置为true并查看是否有帮助。如果没有检查您绑定GridView的方式,请确保在nullPage_Load函数运行之前,不再将其重新绑定到DeleteRows值。发布你如何绑定,这将有助于诊断问题。

我最近也发布了一个类似问题的答案,你可以在这里找到: iterate through gridview rows on button click