datagrid,datalist,repeater的使用

本文介绍ASP.NET中如何使用DataGrid和DataList进行数据操作,包括更新、删除和分页显示等功能,并展示了如何实现横向数据展示及弹出确认窗口的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一。获取模板列中的控件
 1。foreach(DataGridItem dgItem in this.DataGrid1.Items)
    {
     CheckBox chk = (CheckBox)dgItem.FindControl("chkDelete");
     if(chk.Checked ==false)
     {
      chk.Checked = true;
     }
    }
2。for (int i=0; i < this.dgCart.Items.Count; i++)
   {
// 找到某行的数量信息和删除信息。
    TextBox quantityTxt = (TextBox) dgCart.Items[i].FindControl("Quantity");
    CheckBox remove = (CheckBox) dgCart.Items[i].FindControl("Remove");             
    // 出错处理。防止用户的非法输入,如quanlity为负数等
    int quantity;
    try
    {
     quantity = Int32.Parse(quantityTxt.Text);
      Label productId = (Label) dgCart.Items[i].FindControl("bookID");
      //数量为0或用户选择删除
     if (quantity == 0 || remove.Checked == true)
     {
     cart.DeleteItem(cartID, Int32.Parse(productId.Text));
     }
     else
     {
      cart.UpdateItem(cartID, Int32.Parse(productId.Text),quantity);
     }

    }
    catch
    {
     lblError.Text = "对不起,您的输入信息有误!";
    }
   }

三. 分页。
1。datagrid分页。如果是datagrid自带的分页的话,必须在DataGrid1_PageIndexChanged中添加
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
this.BindGrid(bclassId);
或者是:
void BindGrid()
  {
   string bclassId = this.ddlClass.SelectedValue;
   BLL.Product product = new admin.BLL.Product();
   DataSet ds = new DataSet();
   ds = product.GetProductList1(bclassId);
   this.DataGrid1.AllowPaging = true;
   this.DataGrid1.DataSource = ds.Tables[0].DefaultView;
   this.DataGrid1.DataBind();
   this.DataGrid1.DataKeyField = "ProductId";
   this.ShowState();
           
            
  }
  void ShowState()
  {
   this.lblCurrPage.Text = "第"+Convert.ToString(this.DataGrid1.CurrentPageIndex+1)+"页";
   this.lblPageCount.Text = "共"+this.DataGrid1.PageCount+"页";

  }
  public  void  PageButtonClick(object sender, System.EventArgs e)
  {
   string arg = ((LinkButton)sender).CommandName.ToString();
   switch(arg)
   {
    case "next":
     if(this.DataGrid1.CurrentPageIndex<this.DataGrid1.PageCount-1)
     {
      this.DataGrid1.CurrentPageIndex +=1;
     }
     break;
    case "prior":
     if(this.DataGrid1.CurrentPageIndex>0)
     {
      this.DataGrid1.CurrentPageIndex -=1;
     }
        break;
    case "last":
                    this.DataGrid1.CurrentPageIndex = this.DataGrid1.PageCount-1;
     break;
    default:
     this.DataGrid1.CurrentPageIndex = 0;
     break;
   }
   this.BindGrid();
   this.ShowState();
2。datalist分页:
PagedDataSource ps = new PagedDataSource();
   ps.DataSource = ds.Tables[0].DefaultView;
   ps.AllowPaging = true;
   ps.PageSize = 3;
   ps.CurrentPageIndex =currPage - 1;

四。数据横向显示:
<asp:DataList id="DataList1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal">
           <ItemTemplate>
            <table>
             <tr>
              <td><a href ='Product.aspx?productId=<%# DataBinder.Eval(Container.DataItem,"ProductId")%>'>
                <img src ='Images/product/<%# DataBinder.Eval(Container.DataItem,"PPhoto")%>'></a></td>
             </tr>
             <tr>
              <td><a href ='Product.aspx?productId=<%# DataBinder.Eval(Container.DataItem,"ProductId")%>'>
                <%# DataBinder.Eval(Container.DataItem,"ProductName")%>
               </a>
              </td>
             </tr>
            </table>
           </ItemTemplate>

五。弹出确定窗口:
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {
   if(e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
   {
    ((LinkButton)(e.Item.Cells[3].Controls[0])).Attributes.Add("onclick","return confirm('确定要删除么?')");
   }
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值