我有自定義上傳控件。該控件具有帶上載文檔的gridview,第一列有用於通過單擊「刪除」按鈕選擇要刪除的文檔的複選框。上傳控件包含在其他用戶控件中,幾乎在應用程序的任何地方都可以正常工作,除了在一個控件上。問題是當我檢查某個文檔以進行刪除時,代碼執行復選框的checked屬性爲false。在gridview中檢查複選框,在c#codebehind checked property中是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>
,這是刪除按鈕事件
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);
}
檢查您有控制權的頁面上是否啓用了viewstate。檢查頁級和控制級.. – 2010-08-30 09:21:40