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

给DataList模板中的LINKBUTTON增加一个属性
我在DataList模板中放了一个LINKbutton,想点击LINKBUTTON时,弹出一个对话框,问是否真的要删除
代码为:
  if   (!IsPostBack)
                {
                      LinkButton   lbtn   =   (LinkButton)Page.Form.FindControl( "lb_delete ");
                        if   (lbtn   !=   null)
                        {
                                lbtn.Attributes.Add( "onclick ",   "return   confirm( '你确定要删除当前这个组的照片吗? ') ");
                        }
这样写,为什么找不到呢?返回一个空值NULL
把page.Form换成DataList1也不行啊,哪位高手帮我看看啊,谢了啊
弄得了,马上给分

------解决方案--------------------
<%@ Page Language= "C# " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<script runat= "server ">

protected void LinkButton1_Click(object sender, EventArgs e)
{

}

protected void Page_Load(object sender, EventArgs e)
{

}

protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
{
switch (e.Item.ItemType)
{
case ListItemType.Item:
case ListItemType.EditItem:
case ListItemType.AlternatingItem:
case ListItemType.SelectedItem:
LinkButton myDeleteButton = (LinkButton)e.Item.FindControl( "LinkButton1 ");
myDeleteButton.Text = "删除 ";
myDeleteButton.Attributes.Add( "onclick ", "return confirm( '你确认删除此项码? '); ");
break;
}
}

protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
//这里写删除的语句 。我就不写了。
}
</script>

<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> 无标题页 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:DataList ID= "DataList1 " runat= "server " DataKeyField= "areaID " DataSourceID= "SqlDataSource1 "
Width= "375px " OnItemCreated= "DataList1_ItemCreated " OnDeleteCommand= "DataList1_DeleteCommand ">
<ItemTemplate>
id:
<asp:Label ID= "idLabel " runat= "server " Text= ' <%# Eval( "id ") %> '> </asp:Label> <br />
areaID:
<asp:Label ID= "areaIDLabel " runat= "server " Text= ' <%# Eval( "areaID ") %> '> </asp:Label> <br />
area:
<asp:Label ID= "areaLabel " runat= "server " Text= ' <%# Eval( "area ") %> '> </asp:Label> <br />
father:
<asp:Label ID