我有一个带有2个按钮的数据绑定字段的数据列表。我想启用禁用按钮,具体取决于每行的列(状态)值,因此,例如,如果state的值为0,则应禁用remove按钮,并且当state的值为1时,应同样启用add按钮,反之亦然..
protected void dlEditCaravans_ItemDataBound(object sender, DataListItemEventArgs e)
{
Button addtoFeauture = e.Item.FindControl("btnAddToFeature") as Button;
Button removetoFeauture = e.Item.FindControl("btnRemoveFeature") as Button;
int id = Convert.ToInt32(dlEditCaravans.DataKeys[e.Item.ItemIndex]);
int check = caravans.GetfeautureValue(id);
if (check == 0)
{
addtoFeauture.Enabled = true;
}
else
{
removetoFeauture.Enabled = true;
}
}
我尝试了类似上面的内容,但它没有将对象引用设置为实例错误。
答案 0 :(得分:1)
void DataListProduct_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
Button BT = e.Item.FindControl(“ButtonID“) as Button;
BT.Enable = True or false depends upon your condition
}
}
答案 1 :(得分:1)
您需要使用Datalist的ItemDatabound事件
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
DataRow dr = ((DataRowView)e.Item.DataItem).Row;
if (Convert.ToBoolean(dr["StateColumnName"])
{
((Button)e.Item.FindControl("Button1")).Enable = True;
}
}
}