现在的位置: 首页 > 综合 > 正文

ASP.NET 2.0 GridView的RowCommand事件中取得行索引

2013年06月27日 ⁄ 综合 ⁄ 共 3588字 ⁄ 字号 评论关闭
ASP.NET2.0中的GRIDVIEW控件是一个新增的控件。在GRIDVIEW里,行索引被放在了CommandArgument里面,而不是像DataGrid那样可以利用this.MyDataGrid.DataKeys[e.Item.ItemIndex].ToString()方便的取出主键值。
 
如下是一个GridView 的示例代码:
        <asp:GridView ID="grdFileList" runat="server" 
        CssClass="listing highlightTable" GridLines="None" Width="98%" DataKeyNames="fileFullname" 
        AutoGenerateColumns="False" onrowcommand="grdFileList_RowCommand">
            <Columns>
                <asp:HyperLinkField DataNavigateUrlFields="fileURLPath" 
                    DataNavigateUrlFormatString="{0}" DataTextField="filename" HeaderText="文件名" />
                <asp:BoundField DataField="filesize" HeaderText="文件大小(KB)" />
                <asp:BoundField DataField="createdate" HeaderText = "上传日期" />
                <asp:TemplateField HeaderText="操作">
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkDelete" runat="server" CausesValidation="false" 
                            CommandName="DeleteFile" Text="删除" OnClientClick="return confirm(’确认要删除吗?’);"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:ButtonField CommandName="ButtonFile" Text="Button" />
            </Columns>
    </asp:GridView>
 
如果使用默认的CommandField,如下代码:
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
或者ButtonField,代码如下:
<asp:ButtonField CommandName="ButtonFile" Text="Button" />    
 
则可以在RowCommand中使用如下代码取出行号:
if (e.CommandName == "ButtonFile")
{
     int index = Convert.ToInt32(e.CommandArgument); //取的行索引
}
 
但是,CommandField / ButtonField 在某些情况下可能不能满足需要,我们则可以使用模版列来订制操作按钮,代码如下:
<asp:TemplateField HeaderText="操作">
    <ItemTemplate>
                        <asp:LinkButton ID="lnkDelete" runat="server" CausesValidation="false" 
                            CommandName="DeleteFile" Text="删除" OnClientClick="return confirm(’确认要删除吗?’);"></asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>
 
现在 Convert.ToInt32(e.CommandArgument)中e.CommandArgument转换为字符串为空,没有正确获取到行索引 RowIndex。
 
针对上述问题,有如下2种解决办法:
方法一:获取LinkButton 点击所在的GridViewRow,然后获取RowIndex。示例代码如下:
if (e.CommandName == "DeleteFile")
{
   // catching the row in which the link button is clicked.
   GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
   // 获取到行索引 RowIndex
   int index = gvrow.RowIndex;
}
 
方法二:可以利用RowCreated事件来为模版列中LinkButton写入CommandArgument事件。
呈现 GridView 控件之前,必须先为该控件中的每一行创建一个 GridViewRow 对象。在创建 GridView 控件中的每一行时,将引发 RowCreated 事件。这使我们可以提供一个这样的事件处理方法,即每次发生此事件时都执行一个自定义例程(如在行中添加自定义内容,当然也可以添加e.CommandArgument属性为模版列里的LinkButton)。
 
下面的代码示例演示如何使用 RowCreated 事件将正在创建的行的索引存储在该行中所包含的 LinkButton 控件的 CommandArgument 属性中。这允许您确定在用户单击 LinkButton 控件按钮时包含该控件的行的索引。
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
   {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Retrieve the LinkButton control from the first column.
            LinkButton LinkButton1 = (LinkButton)e.Row.FindControl("LinkButton1");
           // Set the LinkButton’s CommandArgument property with the row’s index.
            LinkButton1.CommandArgument = e.Row.RowIndex.ToString();
        }
    }
 
然后,在RowCommand 事件中,采用与前面一直的方法e.CommandArgument 来获取RowIndex。示例代码如下:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowIndex = int.Parse(e.CommandArgument.ToString());
}
 
References:
1.  Getting Row index in Rowcommand event
http://www.dotnetspider.com/forum/159821-Getting-Row-index-Rowcommand-event.aspx
2. ASP.NET2.0中的GRIDVIEW控件在使用TemplateField中的LinkButton时如何在RowCommand事件中找到当前行index的方法
http://www.cnblogs.com/xh3/archive/2006/07/01/440469.html

抱歉!评论已关闭.