开发者

Checked checkbox in gridview, in c# codebehind checked property is false

I have custom upload control. The control has gridview with the uploaded documents and the first column has checkbox for selecting documents I want to delete by clicking Delete button. The upload control is contained in other UserControls almost everywhere in the application and works properly, except at one control. The problem is when I check some document for deleting, when the code executes the checked property of the checkbox is false.

<asp:GridView ID="gvFiles" runat="server" AutoGenerateColumns="false" ShowHeader="true"
    CssClass="DataGrid" Width="100%" OnRowDataBound="gvFiles_RowDataBound">
    <HeaderStyle HorizontalAlign="left" CssClass="UploadControlGridHeader" />
    <RowStyle CssClass="dlcell" />
    <EditRowStyle CssClass="dlsell" />
    <Columns>
        <asp:TemplateField HeaderText="Delete">
            <ItemStyle Width="8%" HorizontalAlign="Center" />
            <ItemTemplate>
                <asp:CheckBox ID="chkFile" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView> 

and this is the delete button event

开发者_开发问答protected void btnDelete_Click(object sender, EventArgs e)
{
    for (int i = 0; i < gvFiles.Rows.Count; i++)
    {
        CheckBox chk = (CheckBox)gvFiles.Rows[i].Cells[0].FindControl("chkFile");
        if (chk.Checked)
        {
            // If file is marked/checked to be deleted
            // then delete it.
            DeleteFile(i);
        }
    }
    Session[KEY_VIEWSTATEFILES] = this.FileList;

    // Repopulate gridview
    BindToGridView();

    // Call command to keep same visible screen.
    CustomCommandEventArgs command = new CustomCommandEventArgs(COMMAND_FILE_DELETED, null);
    OnHappyCommand(command);
}


Not having your DeleteFile(i) code, I can't be sure, but I will take a guess that into that function you remove/delete the row. Am I right? If yes, then it change the index of all your rows, which make you think it is not checked when you check the row that you remember to have checked. You will have to use a backward for loop to fix this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜