代码如下:
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {   


///首先在DATAGRID控件找到删除按钮
ImageButton WysDelete = (ImageButton)e.Item.FindControl("WysDelete"); if (WysDelete!=null)
{
//为每个删除按钮添加对话框属性
WysDelete.Attributes.Add("onclick","javascript:return confirm('你确定要删除选择的新闻?');");
}

}
当我点中datagrid这个删除按钮.发现没有办法处理这个事件.单步测试过程中就发现没有执行到这一步.在page_load()事件执行完后就退出了。
郁闷中~~~~~~~~

解决方案 »

  1.   

    move the code inside ItemCreated event handler instead
      

  2.   

    我放在OnItemDataBound的事件中就可以啊。
    为什么上面的方法就不行呢?
    哪里错误
      

  3.   

    >>>我放在OnItemDataBound的事件中就可以啊。什么意思?如果DataGrid1_ItemDataBound不是跟ItemDataBound事件相关,你怎么能看到confirm?把你其他的编码贴出来,还有,确认你是在
    if (!IsPostBack)
    {
     //......
    }里绑定DataGrid1的
      

  4.   

    我就改了这里:<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" OnItemDataBound="ItemDataBound" runat="server">
    本来是没有OnItemDataBound="ItemDataBound"
    我直接调用DataGrid_ItemDataBound(object sender,....)的.但是一直无法成功.
    就在加了OnItemDataBound="ItemDataBound"后.
    我就直接用:
    public void ItemDataBound(object sender,...)
    就可以咯。
      

  5.   

    if(!Page.IsPostBack)
    {
    try
    {
    OleDbConnection myConnection = new OleDbConnection(DataBaseDB.ConnectionString);
    String cmdText="Select top 15 NewsID,Title,Pubdate from News ";
    OleDbDataAdapter OleDbAda = new OleDbDataAdapter(cmdText,myConnection);
    myConnection.Open();
    DataSet ds = new DataSet();
    OleDbAda.Fill(ds);
    DataGrid1.DataSource=ds;
    DataGrid1.DataBind();
    myConnection.Close();
    }
    catch(Exception ex) {
    Response.Write(ex);
    }
    }
    这个是我绑定数据的
    下面这个是事件驱动:
     public void ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {   


    ///首先在DATAGRID控件找到删除按钮
    ImageButton WysDelete = (ImageButton)e.Item.FindControl("WysDelete");
    //ImageButton WysDelete =(ImageButton)e.Item.Cells[4].Controls[0];
    if (WysDelete!=null)
    {
    //为每个删除按钮添加对话框属性
    WysDelete.Attributes.Add("onclick","return confirm('你确定要删除选择的新闻?');");
    }

    }
      

  6.   

    you have to hook the handler with DataGrid's ItemDataBound event, either throughOnItemDataBound="ItemDataBoundHandlerMethodName" in aspx/ascx pagesorDataGrid1.ItemDataBound += new DataGridItemEventHandler(ItemDataBoundHandlerMethodName);also try in your template....
    <asp:Button id="WysDelete" runat="server" OnClick="DeleteClicked" ... />
    void DeleteClicked(Object sender, EventArgs e)
    {
      
      Button btn = (Button)sender;
      DataGridItem dgi = (DataGridItem)btn.Parent.Parent;  Page.Response.Write("delete button on row " + dgi.ItemIndex.ToString() + " is clicked<BR>");
    }