GridView常用代码 (转)

GridView常用代码
1. GridView1_RowDataBound

protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)

 if(e.Row.RowType == DataControlRowType.DataRow)
 {
  LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
  l.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to delete this record " + DataBinder.Eval(e.Row.DataItem, "CategoryID") + "')");
 }
}

2. GridView1_RowCommand

protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
{
 if (e.CommandName == "Delete")
 {
  // get the categoryID of the clicked row
  int categoryID = Convert.ToInt32(e.CommandArgument);    // Delete the record    
  DeleteRecordByID(categoryID);    // Implement this on your own :)
 }
}

3. GridView1_RowDeleting

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
 int categoryID = (int) GridView1.DataKeys[e.RowIndex].Value;
 DeleteRecordByID(categoryID);
}

4.  CommandArgument, CommandName

<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("CategoryID") %>' CommandName="Delete" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
(在GridView1_RowCommand里,有他们的用法)

原文地址:https://www.cnblogs.com/RobotTech/p/590985.html