日期:2014-05-19  浏览次数:20479 次

DataGrid外的按钮事件无法获取DataGrid的信息,菜鸟救助!
在一个用户控件中我添加了一个Datagrid和一个button控件,DataGrid的数据是从数据库中获取的,在按钮事件中我添加了如下程序:
int   i,count;
count=Datagrid1.Items.Count;
for(i=0;i <count;i++)
{
  CheckBox   Cbox=(CheckBox)Datagrid1.Items[i].Cells[0].FindControl( "CheckBox1 ");
if(Cbox.Checked==true)
{
PurviewSettings.SetMgPurview(Lname.Text,DGdeptlist.Items[i].Cells[1].Text,1);
}
else
{
PurviewSettings.SetMgPurview(Lname.Text,DGdeptlist.Items[i].Cells[1].Text,0);
}
}
我测试的时候,DataGrid中已经有四行数据,可是按钮事件中无法获取Datagrid行数的信息,count变量老是0。那们帮一下啊!!


------解决方案--------------------
什么事件?
------解决方案--------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable DT = new DataTable();
DT.Columns.Add( "ID ", typeof(int));
DataRow DR = DT.NewRow();
DR[ "ID "] = 1;
DT.Rows.Add(DR);
DR = DT.NewRow();
DR[ "ID "] = 2;
DT.Rows.Add(DR);
DR = DT.NewRow();
DR[ "ID "] = 3;
DT.Rows.Add(DR);
DR = DT.NewRow();
DR[ "ID "] = 4;
DT.Rows.Add(DR);
DataGrid1.DataSource = DT;
DataGrid1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < DataGrid1.Items.Count; i++)
{
DataGrid1.Items[i].Cells[0].Text = i.ToString();
}
}
=================================
完全可以,你看一下你什么地方作的不对.