DataGrid模板列应用——在DataGrid中用CheckBox控制TextBox的Enabled属性

将模板列的 CheckBox 的 AutoPostBack 属性设置为 true ,因为在模板列中的控件没有事件,故在 aspx 页面写事件程序,同时将 CheckBox 的 OnCheckedChanged 事件设置为所需要的事件程序。示例如下:

void Check_Change(Object sender, EventArgs e)

{

TextBox txtDescription;

foreach(DataGridItem item in ItemsGrid.Items)

{

selection = (CheckBox)item.FindControl("chkEnabled");

txtDescription = (TextBox)item.FindControl("txtDescription");

if (selection.Checked) { txtDescription.Enabled=false; }

else { txtDescription.Enabled=true;}

}

}

这样,当选中CheckBox时,TextBox中的内容将不可编辑,这个例子的缺陷在于没有使用就javascript灵活,因为每次都要与服务器通信。

Published At
Categories with Web编程
Tagged with
comments powered by Disqus