日期:2014-05-18  浏览次数:20415 次

要疯了,想了几个小时了,DataList中ItemDataBind 寻找控件的问题!
当DataList 绑定了控件以后,触发ItemDataBind事件,我需要在触发该事件的时候找到

该次绑定的模板下的 控件(比如CheckBox),但是时机总是错位,郁闷!
代码如下~请问高手们这个问题如何解决啊~~
C# code

  protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            if (DataList1.Items.Count > 1)
            {
                foreach (DataRowView var in dv)
                {

                    if (var["m_id"].ToString() == this.DataList1.DataKeys[e.Item.ItemIndex].ToString())
                    {


                        (DataList1.Items[int.Parse(ViewState["count"].ToString()) + 2].FindControl("CheckBox5") as CheckBox).Checked = true;
                        (DataList1.Items[int.Parse(ViewState["count"].ToString()) + 2].FindControl("CheckBox1") as CheckBox).Checked = bool.Parse(var["select"].ToString());
                        (DataList1.Items[int.Parse(ViewState["count"].ToString()) + 2].FindControl("CheckBox2") as CheckBox).Checked = bool.Parse(var["insert"].ToString());
                        (DataList1.Items[int.Parse(ViewState["count"].ToString()) + 2].FindControl("CheckBox3") as CheckBox).Checked = bool.Parse(var["update"].ToString());
                        (DataList1.Items[int.Parse(ViewState["count"].ToString()) + 2].FindControl("CheckBox4") as CheckBox).Checked = bool.Parse(var["delete"].ToString());

                    }
                }
            }
            else
            {
                return;
            }
            ViewState["count"] = int.Parse(ViewState["count"].ToString()) + 1;
        }
    }








------解决方案--------------------
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
CheckBox cb = e.Item.FindControl("CheckBox的id") as CheckBox;
//....cb就是每一行这个id的那个控件
}
------解决方案--------------------
探讨
if (e.Item.ItemType == ListItemType.Item ¦ ¦ e.Item.ItemType == ListItemType.AlternatingItem)
{
CheckBox cb = e.Item.FindControl("CheckBox的id") as CheckBox;
//....cb就是每一行这个id的那个控件
}

------解决方案--------------------
探讨
if (e.Item.ItemType == ListItemType.Item ¦ ¦ e.Item.ItemType == ListItemType.AlternatingItem)
{
CheckBox cb = e.Item.FindControl("CheckBox的id") as CheckBox;
//....cb就是每一行这个id的那个控件
}

------解决方案--------------------
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{

CheckBox chk;
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 

chk = (CheckBox)e.Item.FindControl("Checkbox的ID"); 

}