一。獲取模板列中的控件
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('確定要刪除麼?')");
}
}