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

来源:互联网 发布:网络有利有弊 英语翻译 编辑:程序博客网 时间:2024/06/05 03:52

------.aspx  添加:OnRowCreated="GridView1_RowCreated"
    <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/Base.mdb"
            SelectCommand="SELECT [id], [Title], [Classid] FROM [leoWeb_News]"
            DeleteCommand="delete from leoweb_news where id = @id"
        ></asp:AccessDataSource>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            DataKeyNames="id" DataSourceID="AccessDataSource1" OnRowCreated="GridView1_RowCreated" >
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
                    SortExpression="id" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="Classid" HeaderText="Classid" SortExpression="Classid" />
                <asp:CommandField ShowDeleteButton="True" />               
            </Columns>
        </asp:GridView>

------------------------------------------------------------------------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" 时 onclick="return window.confirm('删除确认');" A标记回加上上面这个属性。
如果:ButtonType="Button"  则 onclick="return window.confirm('删除确认');javascript:__doPostBack('GridView1','Delete$9')" 可以看到这个属性被加到了原来的onclick之前。

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

原创粉丝点击