为GridView的删除行添加删除确认

------.aspx  添加:OnRowCreated="GridView1_RowCreated"
                SelectCommand="SELECT [id], [Title], [Classid] FROM [leoWeb_News]"
            DeleteCommand="delete from leoweb_news where id = @id"
        >

                    DataKeyNames="id" DataSourceID="AccessDataSource1" OnRowCreated="GridView1_RowCreated" >
           
                                    SortExpression="id" />
               
               
                               
           

       

------------------------------------------------------------------------cs 添加

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row != null && e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lb = e.Row.Cells[3].Controls[0] as LinkButton;

            //如果是Button按钮,即 ButtonType="Button" 时
            //则 Button lb = e.Row.Cells[3].Controls[0] as Button;

            lb.Attributes.Add("onclick", "return window.confirm('删除确认')");

        }
    }

 ------------------------------------------------------------------------------------------------

在最终的HTML结果源文件中,ButtonType="LinkButton" 时 οnclick="return window.confirm('删除确认');" A标记回加上上面这个属性。
如果:ButtonType="Button"  则 οnclick="return window.confirm('删除确认');javascript:__doPostBack('GridView1','Delete$9')" 可以看到这个属性被加到了原来的onclick之前。

知道这些对了解.net2.0控件执行机理的帮助是很大的。 

你可能感兴趣的:(.NET控件)