1、控件行中有时包含删除按钮,为基添加删除确认对话框
//查找控件并为其注册一段脚本
System.Web.UI.WebControls.LinkButton lnkb = (System.Web.UI.WebControls.LinkButton)e.Item.FindControl("lnkbDel");
if (lnkb != null)
{
lnkb.Attributes.Add("onclick", "return confirm('确认要删除该条记录吗?');");
}
System.Web.UI.WebControls.LinkButton lnkb = (System.Web.UI.WebControls.LinkButton)e.Item.FindControl("lnkbDel");
if (lnkb != null)
{
lnkb.Attributes.Add("onclick", "return confirm('确认要删除该条记录吗?');");
}
2、有时编辑列包含比较复杂的数据绑定控件,比如DropdownList
//在ItemDataBound事件中进行绑定
protected void dlstPage_ItemDataBound(object sender, DataListItemEventArgs e)
{
//这句是关键,判断当ItemType为编辑列时进行绑定
if (e.Item.ItemType == ListItemType.EditItem)
{
//此处用FindControl方法绑定控件,略
}
}
protected void dlstPage_ItemDataBound(object sender, DataListItemEventArgs e)
{
//这句是关键,判断当ItemType为编辑列时进行绑定
if (e.Item.ItemType == ListItemType.EditItem)
{
//此处用FindControl方法绑定控件,略
}
}
//GridView中有所不同
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList drop = (DropDownList)e.Row.FindControl("dropParent");
if (drop != null)
{
drop.DataSource = dt;
drop.DataValueField = "pg_id";
drop.DataTextField = "pg_tag";
drop.DataBind();
//这句具有划时代的意义,再也不用FindControl了,直接获取绑定列的值
drop.SelectedValue = DataBinder.Eval(e.Row.DataItem, "parent").ToString();
}
}
}
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList drop = (DropDownList)e.Row.FindControl("dropParent");
if (drop != null)
{
drop.DataSource = dt;
drop.DataValueField = "pg_id";
drop.DataTextField = "pg_tag";
drop.DataBind();
//这句具有划时代的意义,再也不用FindControl了,直接获取绑定列的值
drop.SelectedValue = DataBinder.Eval(e.Row.DataItem, "parent").ToString();
}
}
}
3、
4、