gridview

增加按钮或操作

1.将按钮的 CommandName 属性设置为标识其功能的字符串,如“打印”或“复制”。

2.如果使用的是 TemplateField 对象并且必须在事件处理程序方法中访问行索引,则将按钮的CommandArgument 属性设置为标识当前行的表达式。

下面的示例演示如何将 TemplateField 列中某个按钮的CommandArgument 属性设置为当前行索引。 在该示例中,该列包含一个显示购物车的 Button 控件

<asp:TemplateField>
  <ItemTemplate>
    <asp:Button ID="AddButton" runat="server" 
      CommandName="AddToCart" 
      CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
      Text="Add to Cart" />
  </ItemTemplate> 
</asp:TemplateField>

GridView 控件的RowCommand 事件创建一个方法。在该方法中,执行下列操作:

1. 检查事件参数对象的 CommandName 属性来查看传入什么字符串。

2. 如果需要,使用 CommandArgument 属性检索包含该按钮的行的索引。

3. 为用户单击的按钮执行相应的逻辑。

下面的示例演示响应 GridView 控件中的按钮单击的方法。在该示例中,TemplateField 列中的按钮发送命令“AddToCart”。RowCommand 事件处理程序确定被单击的按钮。如果被单击的是购物车按钮,则代码执行相应的逻辑。

protected void GridView1_RowCommand(object sender, 
  GridViewCommandEventArgs e)
{
  if (e.CommandName == "AddToCart")
  {
    // Retrieve the row index stored in the 
    // CommandArgument property.
    int index = Convert.ToInt32(e.CommandArgument);

    // Retrieve the row that contains the button 
    // from the Rows collection.
    GridViewRow row = GridView1.Rows[index];

    // Add code here to add the item to the shopping cart.
  }

  }

GridView序号问题

<asp:BoundField  HeaderText="序号" />
或用
<asp:TemplateField HeaderText="序号">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>

protected void GridView1_RowDataBond(object sender, GridViewRowEventArgs e)
{
         if (e.Row.RowIndex >= 0)
         {
          e.Row.Cells[0].Text = Convert.ToString(e.Row.RowIndex + 1);
         }          
   }

或直接用页面输出

<asp:TemplateField HeaderText="序号">
<ItemTemplate>
   <%# Container.DataItemIndex + 1%>
 </ItemTemplate>
</asp:TemplateField>

自带分页方法

<asp:TemplateField HeaderText="序号">
<ItemTemplate>
<%# this.GridView1.PageIndex  * this.GridView1.PageSize
         + GridView1.Rows.Count + 1%>
</ItemTemplate>
</asp:TemplateField>
AspNetPager分页
<asp:TemplateField HeaderText="序号">
<ItemTemplate>
   <%# (this.Pager1.CurrentPageIndex - 1) * this.Pager1.PageSize

    + Container.DataItemIndex + 1%>
</ItemTemplate>
</asp:TemplateField>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值