我用后台代码在DataGrid控件的页脚列里,加入了一个CheckBox服务端对象,但提交的时候,我用如下语句求取,出错,提示"未将对象引用设置到对象的实例"
CheckBox bolEdit = (CheckBox)this.DataGrid1.FindControl("bolEdit");
this.Label1.Text = bolEdit.Checked.ToString();请问该如何求取?
CheckBox bolEdit = (CheckBox)this.DataGrid1.FindControl("bolEdit");
this.Label1.Text = bolEdit.Checked.ToString();请问该如何求取?
if(e.Item.ItemType==ListItemType.Foot)
{
CheckBox bolEdit = (CheckBox)this.DataGrid1.FindControl("bolEdit");
this.Label1.Text = bolEdit.Checked.ToString();
}
{
CheckBox CheckBoxSelect=(CheckBox)DataGridName.Item[i].FindContrl("CheckBoxName");
if(CheckBoxSelect.Checked)//选取选中的checkbox
{
...
}
...
}
你的方法不能用。
以这种方式,只能求取到DATAGRID所生成的普通项及交替项,无法求取页眉及页脚
比如说在页面放一个提交按钮,之后在该提交按钮的事件里,求取上述控件的值.
---------------------------------------
遍历datagriditem:
foreach (DataGridItem e in datagrid1.Items)
{
if(e.Item.ItemType==ListItemType.Foot)
{
CheckBox bolEdit = (CheckBox)this.DataGrid1.FindControl("bolEdit");
string result = bolEdit.Value;//?
}
}
各种方法都试过了,当然,在ItemDataBound事件里面是可以得到,但要对控件做操作的时候,往往是在某个提交按钮事件里面,恰恰是在这里卡壳了.
请各位一起探讨吧
{
if(C.ID = "你控件的ID")
{
}
}
If e.Item.ItemType = ListItemType.Footer Then
Dim lbl As Label = CType(e.Item.FindControl("Label2"), Label)
lbl.Text = Now
End If
End Sub
Dim ddl As Label = CType(item.FindControl("label2"), Label) Me.Response.Write(ddl.Text)
End If
Next这个试
你的最后一个方法思路是对的(但所有其他方法都是错的).我受到你的启发,写如下程序正确求取出值.
int mycount = this.DataGrid1.Controls[0].Controls.Count;
for(int i=0;i<mycount;i++)
{
DataGridItem myitem = (DataGridItem)this.DataGrid1.Controls[0].Controls[i];
TextBox mybox = new TextBox();
mybox = (TextBox)myitem.FindControl("TextBox1");
if(mybox != null)
{
this.MyScript(i.ToString() + "=" + mybox.Text + " 总行数:" + mycount.ToString());
}
}
以上原理,推测应是如下:
datagrid控件在生成之后,作为父控件,在其下拥有直属的一个子控件,而在此子控件之下,又拥有8个孙控件,此8个孙控件,包括通常的页眉/普通项/交替项/页脚等.求取页脚内的控件,自然是最后一个孙控件,所以求取的方法可简要归纳为如下:int mycount = this.DataGrid1.Controls[0].Controls.Count;
TextBox mybox = (TextBox)this.DataGrid1.Controls[0].Controls[mycount-1].FindControl("TextBox1");
this.MyScript(mybox.Text);
谢谢.
此贴暂放几天,以供各位同仁参考.几天后结贴.主要得分应是 noyester(继续是菜鸟)
foreach (DataGridItem e in datagrid1.Items)
{
if(e.Item.ItemType==ListItemType.Foot)
{
CheckBox bolEdit = (CheckBox)this.DataGrid1.FindControl("bolEdit");
string result = bolEdit.Value;//?
}
}
求取的行控件,总数只有6个,可能是少了页眉与页脚.所以无法正确求取到值