一个DataGrid难题:在编辑时,如何使某些模板列中的控件处于不可编辑状态? <asp:TextBox id="text1" ReadOnly=True></asp> 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼上的是一刀切呀,那这一列都不能编辑了,我的意思是:1)、当模板列3的值为888时,模板列2和模板列4在编辑时处于不可编辑状态;2)、当模板列3的值为666时,模板列2和模板列4在编辑时处于可编辑状态;即模板列2和模板列4的ReadOnly属性要根据模板列3的值来确定! 在编辑事件中判断模板列3的值如果是888,设置textbox 的ReadOnly=True或enable=false就可以了。 如果模板列3中使用的是TextBox控件,2和4中也是,那么可以如下:if (((TextBox)DataGrid.Items[DataGrid.SelectedIndex].FindControl("TextBox3")).Text == "888"){((TextBox)DataGrid.Items[DataGrid.SelectedIndex].FindControl("TextBox2")).ReadOnly = true;((TextBox)DataGrid.Items[DataGrid.SelectedIndex].FindControl("TextBox4")).ReadOnly = true;}else{((TextBox)DataGrid.Items[DataGrid.SelectedIndex].FindControl("TextBox2")).ReadOnly = false;((TextBox)DataGrid.Items[DataGrid.SelectedIndex].FindControl("TextBox4")).ReadOnly = false;}当然,你的模板列3如果不是TextBox控件,比如是Label控件,那么也可以按照同样的办法处理 在数据源里,如果可以根据模板列3的情况加一列bool值就方便,比如enable<asp:TextBox id="text1" ReadOnly='<%# DataBinder.Eval(Container.DataItem, "enable") %>'></asp>做个dataTable吧,比较方便 源程序:private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { if (((TextBox)DataGrid1.Items[DataGrid1.SelectedIndex].FindControl("TextBox3")).Text=="888") { ((TextBox)DataGrid1.Items[DataGrid1.SelectedIndex].FindControl("TextBox2")).ReadOnly=true; ((TextBox)DataGrid1.Items[DataGrid1.SelectedIndex].FindControl("TextBox4")).ReadOnly=true; } else if (((TextBox)DataGrid1.Items[DataGrid1.SelectedIndex].FindControl("TextBox3")).Text=="666") { ((TextBox)DataGrid1.Items[DataGrid1.SelectedIndex].FindControl("TextBox2")).ReadOnly=false; ((TextBox)DataGrid1.Items[DataGrid1.SelectedIndex].FindControl("TextBox4")).ReadOnly=false; }运行出错:说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名:index源错误: 行 112: { 行 113:行 114: if (((TextBox)DataGrid1.Items[DataGrid1.SelectedIndex].FindControl("TextBox3")).Text=="888")行 115: {行 116: 哦,不好意思,忘记了一点东西,如果你是用DataGridSelectedIndex的时候一定要先赋值,否则它默认是"-1",你可以使用DataGrid.SelectedIndex = e.Item.ItemIndex,也可以直接使用e.Item.ItemIndex替换现在程序中的DataGrid1.SelectedIndex 对了,你也可以直接使用e.Item.FindControl来替换DataGrid.Items[index].FindControl 谢谢 newfans(笨猫) ,我再去试试先! newfans(笨猫),我按照你的提示去做了,“索引超出范围”这个错误没有出现了,正欲窃喜中。蹦出这么个错误来:说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 行 113: this.DataGrid1.EditItemIndex = e.Item.ItemIndex;行 114:行 115: if (((TextBox)e.Item.FindControl("TextBox3")).Text=="888")行 116: {行 117: 真是晕呀~~~ 嗯,你的DataGrid是动态绑定的吗?就是说它的AutoGenerateColumns属性是true吗? 哦,不对,上面的一个作废。你的模板列中确实有这样一个控件吗?它的ID是“TextBox3” 有呀,TextBox3就是位于模板列3中的。 嗯,你用e.Item.Cells[3].FindControl("TextBox3")试试 呵呵,试了,一样的。(TextBox)e.Item.Cells[3].FindControl("TextBox3")(TextBox)DataGrid1.Items[DataGrid.SelectedIndex].FindControl("TextBox3")我都试了,一样的错误!继续向高手求解!!! 真是奇怪,我就是这么用的,可以用的呀。我的MSN:[email protected]在这上面聊太麻烦了。 用这个式式(TextBox)e.Item.Cells[3].Control[0]; 问题是这样:在你指定EditItemIndex之后,再次调用你的数据绑定方法,然后使用DataGrid.Items[e.Item.ItemIndex].Cells[].FindControl,就可以得到你想要的控件了。 问题顺利解决!!谢谢 newfans(笨猫) 、 martix(矩阵)、 pigdotnet(IT猪)、 chyich() 、 garth(菜鸟)的参与,特别感谢newfans(笨猫),在他的帮助下顺利地解决了问题。 请教一下大家,现在项目要搞一个brantcache,但是不知道是啥东西 一个郁闷了我一天的问题 奇怪的问题? 在asp.net中加入FLASH。。。 数据库连接问题疑问 一个困惑我很久的问题,不说出来心里难受! 服务器按钮控件提交 提交错误 返回当前页面 怎么让当前页面输入的数据不清空! 如何给DataGridItem 加上背景色??? asp.net如下从数据库中读取数据生成如下格式的XML文件! ASP.NET调试过程中Firebug控制台一只跳不停,看不懂 提一个低级问题! ASP.NET做网站和websphere、weblogic有什么优劣?
2)、当模板列3的值为666时,模板列2和模板列4在编辑时处于可编辑状态;即模板列2和模板列4的ReadOnly属性要根据模板列3的值来确定!
if (((TextBox)DataGrid.Items[DataGrid.SelectedIndex].FindControl("TextBox3")).Text == "888")
{
((TextBox)DataGrid.Items[DataGrid.SelectedIndex].FindControl("TextBox2")).ReadOnly = true;
((TextBox)DataGrid.Items[DataGrid.SelectedIndex].FindControl("TextBox4")).ReadOnly = true;
}
else
{
((TextBox)DataGrid.Items[DataGrid.SelectedIndex].FindControl("TextBox2")).ReadOnly = false;
((TextBox)DataGrid.Items[DataGrid.SelectedIndex].FindControl("TextBox4")).ReadOnly = false;
}当然,你的模板列3如果不是TextBox控件,比如是Label控件,那么也可以按照同样的办法处理
<asp:TextBox id="text1" ReadOnly='<%# DataBinder.Eval(Container.DataItem, "enable") %>'></asp>
做个dataTable吧,比较方便
{ if (((TextBox)DataGrid1.Items[DataGrid1.SelectedIndex].FindControl("TextBox3")).Text=="888")
{
((TextBox)DataGrid1.Items[DataGrid1.SelectedIndex].FindControl("TextBox2")).ReadOnly=true;
((TextBox)DataGrid1.Items[DataGrid1.SelectedIndex].FindControl("TextBox4")).ReadOnly=true;
}
else if (((TextBox)DataGrid1.Items[DataGrid1.SelectedIndex].FindControl("TextBox3")).Text=="666")
{
((TextBox)DataGrid1.Items[DataGrid1.SelectedIndex].FindControl("TextBox2")).ReadOnly=false;
((TextBox)DataGrid1.Items[DataGrid1.SelectedIndex].FindControl("TextBox4")).ReadOnly=false;
}
运行出错:说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名:index源错误:
行 112: {
行 113:
行 114: if (((TextBox)DataGrid1.Items[DataGrid1.SelectedIndex].FindControl("TextBox3")).Text=="888")
行 115: {
行 116:
正欲窃喜中。蹦出这么个错误来:说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 113: this.DataGrid1.EditItemIndex = e.Item.ItemIndex;
行 114:
行 115: if (((TextBox)e.Item.FindControl("TextBox3")).Text=="888")
行 116: {
行 117:
真是晕呀~~~
你的模板列中确实有这样一个控件吗?它的ID是“TextBox3”
在这上面聊太麻烦了。
(TextBox)e.Item.Cells[3].Control[0];