今天在一个项目中的某个页面内添加了一个GRIDVIEW表控件,其中一个字段为模板列,放入了一个CHECKBOX复选框控件,我希望用户在选择复选框时能激发它的CheckedChanged事件,从而进行数据库的操作,代码如下:
ASPX页面代码:
代码
<
asp:GridView ID
=
"
gridCursor
"
runat
=
"
server
"
CssClass
=
"
Grid
"
Width
=
"
90%
"
AutoGenerateColumns = " False " onrowdatabound = " gridCursor_RowDataBound "
DataKeyNames = " 学科代码,状态 " >
< Columns >
< asp:BoundField DataField = " 学科代码 " HeaderText = " 学科代码 " />
< asp:BoundField DataField = " 学科名称 " HeaderText = " 学科名称 " />
< asp:TemplateField HeaderText = " 状态 " >
< ItemTemplate >
< asp:CheckBox ID = " chkValid " runat = " server " AutoPostBack = " True "
oncheckedchanged = " chkValid_CheckedChanged " />
</ ItemTemplate >
</ asp:TemplateField >
</ Columns >
</ asp:GridView >
AutoGenerateColumns = " False " onrowdatabound = " gridCursor_RowDataBound "
DataKeyNames = " 学科代码,状态 " >
< Columns >
< asp:BoundField DataField = " 学科代码 " HeaderText = " 学科代码 " />
< asp:BoundField DataField = " 学科名称 " HeaderText = " 学科名称 " />
< asp:TemplateField HeaderText = " 状态 " >
< ItemTemplate >
< asp:CheckBox ID = " chkValid " runat = " server " AutoPostBack = " True "
oncheckedchanged = " chkValid_CheckedChanged " />
</ ItemTemplate >
</ asp:TemplateField >
</ Columns >
</ asp:GridView >
后台CheckedChanged事件的代码:
代码
protected
void
chkValid_CheckedChanged(
object
sender, EventArgs e)
{
CheckBox checkbox = (CheckBox)sender;
GridViewRow row = (GridViewRow)checkbox.NamingContainer;
string codeCursor = gridCursor.DataKeys[row.RowIndex][ " 学科代码 " ].ToString();
if (checkbox.Checked)
sysSqlRunner.isDone( " update educa_bas_course set status=1 where code=' " + codeCursor + " ' " );
else
sysSqlRunner.isDone( " update educa_bas_course set status=0 where code=' " + codeCursor + " ' " );
}
{
CheckBox checkbox = (CheckBox)sender;
GridViewRow row = (GridViewRow)checkbox.NamingContainer;
string codeCursor = gridCursor.DataKeys[row.RowIndex][ " 学科代码 " ].ToString();
if (checkbox.Checked)
sysSqlRunner.isDone( " update educa_bas_course set status=1 where code=' " + codeCursor + " ' " );
else
sysSqlRunner.isDone( " update educa_bas_course set status=0 where code=' " + codeCursor + " ' " );
}
程序虽然写好了,可以正确编译运行,但奇怪的是CHECKBOX的CheckedChanged事件确怎么也触发不了,在网上找了很多类似问题的解答都无效,最后找到一篇文章说需要把GRIDVIEW的EnableViewState设为false才行,结果一试,果然可以触发CheckedChanged事件了。