我有一個gridview,裏面是一個項目的狀態複選框, 複選框有checkchanged事件,但當我試圖檢查或取消選中它時, checkchanged不會觸發。檢查更改沒有觸發時,在asp.net中的gridview內
這裏是內部提前
<asp:GridView ID="dgvItems" runat="server" OnRowCreated="dgvItems_RowCreated" OnRowDataBound="dgvItems_RowDataBound" OnSelectedIndexChanged="dgvItems_SelectedIndexChanged" OnRowDeleting="dgvItems_RowDeleting">
<Columns>
<asp:TemplateField HeaderText ="Status">
<ItemTemplate>
<asp:CheckBox ID="chkStatus" runat="server" Checked = <%# (string)Eval("item_status") == "Active" ? true : false %> OnCheckedChanged="chkStatus_CheckedChanged" AutoPostBack="True" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void chkStatus_CheckedChanged(object sender, EventArgs e)
{
GridViewRow row = dgvItems.SelectedRow;
string item_id = row.Cells[0].Text.ToString();
con.Open();
SqlCommand cmd = new SqlCommand("Update Items set item_status = 'Inactive' where item_id = @item_id ",con);
cmd.Parameters.AddWithValue("@item_id", cmd);
cmd.ExecuteNonQuery();
con.Close();
}
感謝您與複選框GridView控件的代碼!
您好,感謝! isPostBack防止checkchanged被更改 – Dave 2015-04-01 10:41:45