C#: 
protected void gvGoodsInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((LinkButton)(e.Row.Cells[6].Controls[0])).Attributes.Add("onclick", "return confirm('确定要删除吗?')");
        }
    }
ASPX:
<asp:GridView ID="gvGoodsInfo" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" OnRowDataBound="gvGoodsInfo_RowDataBound" OnRowDeleting="gvGoodsInfo_RowDeleting" AllowPaging="True" PageSize="8" OnPageIndexChanging="gvGoodsInfo_PageIndexChanging" Font-Size="9pt" Width="477px" Height="83px">
                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                        <EditRowStyle BackColor="#999999" />
                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                        <PagerStyle BackColor="#CBCF7A" ForeColor="Black" HorizontalAlign="Right" />
                        <HeaderStyle BackColor="#CBCF7A" Font-Bold="True" ForeColor="White" />
                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                        <Columns>
                            <asp:BoundField DataField="GoodsID" HeaderText="商品ID" />
                            <asp:BoundField DataField="GoodsName" HeaderText="商品名称" />
                            <asp:BoundField DataField="GoodsTypeName" HeaderText="商品类别" />
                            <asp:BoundField DataField="GoodsPrice" HeaderText="商品价格" />
                            <asp:BoundField DataField="GoodsIsNew" HeaderText="是否推荐" />
                            <asp:HyperLinkField HeaderText="详细信息" Text="详细信息" DataNavigateUrlFields="GoodsID" DataNavigateUrlFormatString="EditGoods.aspx?GoodsID={0}">
                                <ControlStyle Font-Underline="False" ForeColor="Black" />
                            </asp:HyperLinkField>
                            <asp:CommandField HeaderText="删除" ShowDeleteButton="True">
                                <ControlStyle Font-Underline="False" ForeColor="Black" />
                            </asp:CommandField>
                        </Columns>
                    </asp:GridView>
请问 C#中if (e.Row.RowType == DataControlRowType.DataRow)和((LinkButton)(e.Row.Cells[6].Controls[0])).Attributes.Add("onclick", "return confirm('确定要删除吗?')");指什么,越详细越好。

解决方案 »

  1.   

    if (e.Row.RowType == DataControlRowType.DataRow)
    就是指这个行是数据行,gridview里在行包括标题行,编辑行等等..所以需要用这个判断行在类型.((LinkButton)(e.Row.Cells[6].Controls[0])).Attributes.Add("onclick", "return confirm('确定要删除吗?')");这个是找到当前行的第7个单元格的第一个控件(返回的可能是object类型或者是control类型,我忘记轼),所以必须向上转换为LinkButton.之后给该控件添加一个特性,就是js
    当删除的时候显示对话框.
      

  2.   

    GridView每一行的數據綁定都會執行到 GridView_RowDataBound 這個方法, 
    "if (e.Row.RowType == DataControlRowType.DataRow)"  這行代碼的意思就是, 過濾掉非數據行, 就是執行到
    表頭 或 表尾, 則不會進入if之內.
    ((LinkButton)(e.Row.Cells[6].Controls[0])).Attributes.Add("onclick", "return confirm('确定要删除吗?')");
    這一行呢, 就是Grid中有個刪除按鈕(linkbutton), 找到這個刪除按鈕, 給它添加一個屬性, 在單擊 "刪除"按鈕
    時出現提示 "确定要删除吗?", 如果點擊 "確定" , 則會返回 "true", 如果點擊 "取消" , 則返回 "false"