用户操作
[留言]  [发消息]  [加为好友] 
订阅我的博客
XML聚合    FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
haoliumeihua的公告
<!-- *** Vdoing Code *** --> <script type="text/javascript" charset="UTF-8" src="http://s.vdoing.com/u/121/62116.js"></script> <noscript><a href="http://www.vdoing.com" title="Vdoing StatsX No.62116"><img src="http://simg.vdoing.com/m/62116/x01.gif?noscript" border="0"></a></noscript> <!-- *** End of Vdoing Code *** -->
文章分类
存档

翻译  gridview使用TemplateField中的LinkButton时如何在RowCommand事件中找到当前行index的方法 收藏

ASP.NET2.0中的GRIDVIEW控件在使用TemplateField中的LinkButton时如何在RowCommand事件中找到当前行index的方法
  ASP.NET2.0中的GRIDVIEW控件真是非常奇怪,不知道MS是怎么考虑的,在GRIDVIEW里,行索引被放在了CommandArgument里面,而不是像DataGrid那样可以利用this.MyDataGrid.DataKeys[e.Item.ItemIndex].ToString()方便的取出主键值,
同时我们注意到,如果使用默认的CommandField,
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
 则可以在RowCommand中使用如下代码取出行号:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//编辑按扭
if (e.CommandName == "Edit")
{
    //设置编辑行高亮显示
    this.GridView1.EditRowStyle.BackColor = Color.FromName("#F7CE90");
//string index= this.GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
    int index = Convert.ToInt32(e.CommandArgument);
    GridViewRow row = GridView1.Rows[index];
    string xh3 = row.Cells[3].Text;
}
}

但问题是,CommandField的可操控性是很底的,我们一般习惯于使用模版列来订制操作按钮,如下:
<asp:TemplateField HeaderText="操作" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="更新"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="编辑" ></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Select" Text="选择"></asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Delete" Text="删除" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>


 随之而来,问题出现了,运行报错:输入字符串的格式不正确, Convert.ToInt32(e.CommandArgument)中e.CommandArgument转换为字符串为空。当我们把CommandField转换为模版列时,默认的CommandArgument属性丢失了!!!
思考了两天,翻阅了网上的资料,最后在MSDN文档中发现,呈现 GridView 控件之前,必须先为该控件中的每一行创建一个 GridViewRow 对象。在创建 GridView 控件中的每一行时,将引发 RowCreated 事件。这使我们可以提供一个这样的事件处理方法,即每次发生此事件时都执行一个自定义例程(如在行中添加自定义内容,当然也可以添加e.CommandArgument属性为模版列里的LinkButton)。
GridViewRowEventArgs 对象将被传给事件处理方法,随之我们可以访问正在创建的行的属性。若要访问行中的特定单元格,可以使用 GridViewRowEventArgs 对象的 Cells 属性。使用 RowType 属性可确定正在创建的是哪一种行类型(标题行、数据行等等)。
好了,原来我们可以利用RowCreated事件来为模版列中LinkButton写入CommandArgument事件。
下面的代码示例演示如何使用 RowCreated 事件将正在创建的行的索引存储在该行中所包含的 LinkButton 控件的 CommandArgument 属性中。这允许您确定在用户单击 LinkButton 控件按钮时包含该控件的行的索引。

/// <summary>
/// 为模版列LinkButton写入CommandArgument事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>

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();
}

}

好了,其余的代码不变,在GridView1_RowCommand中的代码不变.

比如再一个单元格中要加入两个按钮:

.aspx页面:

 <asp:TemplateField HeaderText="导航序号" ShowHeader="False">
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Eval("Modsort") %>'></asp:Label>
                    <asp:ImageButton ID="ImageButton1" runat="server"
                        ImageUrl="/Manager/enterpriseManager/Mod/Images/up.png" Height="18px"
                        Width="17px" CausesValidation="false" CommandName="up" Text="up" />&nbsp;
                    <asp:ImageButton ID="ImageButton2" runat="server"
                        ImageUrl="/Manager/enterpriseManager/Mod/Images/down.png" Height="18px"
                        Width="17px" CausesValidation="false" CommandName="down" Text="down" />
                </ItemTemplate>
                <HeaderStyle BackColor="#C00000" />
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>

cs文件:

在RowCreated事件里面(注意这里很重要)

   protected void GridView_RowCreated(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           ImageButton ImageButton1 = e.Row.FindControl("ImageButton1") as ImageButton;
           ImageButton1.CommandArgument = e.Row.RowIndex.ToString();
           ImageButton ImageButton2 = e.Row.FindControl("ImageButton2") as ImageButton;
           ImageButton2.CommandArgument = e.Row.RowIndex.ToString();
       }
   }

在RowCommand事件里面:

  protected void mod_RowCommand(object sender, GridViewCommandEventArgs e)
    {

 if (e.CommandName == "up")
        {
            ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('我要UP!');</script>");
        }
        if(e.CommandName=="down")
        {
            ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('我要DOWN!');</script>");
        }

}

部分资料来源:http://hi.baidu.com/123413927/blog/item/8c41ce17ab5ea708c83d6d4d.html

发表于 @ 2009年04月22日 11:09:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:动态改变Asp.net页面标题方法整合 | 新一篇:您试图在此 Web 服务器上访问的 Web 应用程序当前不可用

  • 发表评论
  • 评论内容:
  •  
Copyright © haoliumeihua
Powered by CSDN Blog