Custom Paging Gridview and Custom Pager

http://www.4guysfromrolla.com/articles/032206-1.aspx
http://www.dsoftworld.com/2011/02/custom-paging-gridview-inheriting-from.html
http://www.codeproject.com/Articles/20777/A-very-nice-and-complete-custom-GridView-pager (kha dep nhung ko biet co custom duoc ko nua?
http://www.codeproject.com/Articles/11418/Pager-Control-for-ASP-NET (Thang nay cung duoc)
http://www.stereoplex.com/blog/using-output-parameters-with-the-enterprise-librar
http://aspalliance.com/articleViewer.aspx?aId=688&pId=-1

Advertisements

Phân trang trong GridView (Mức Stored Procedure)

Chi tiết tại đây: http://www.codeproject.com/KB/webforms/ASPEnhancedGridView.aspx#

GridView và DetailsView CodeBehind

1. GridView

namespace ASPNETWebApplication.WebAdmin
{
    public partial class Customers : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                // Set the selected menu item in the Master page.
                SelectedMenu = "customers";

                // Set default sort settings.
                SortColumn = "CustomerId";
                SortDirection = "ASC";

                Bind();
            }
        }

        /// <summary>
        /// Sets datasources and bind data.
        /// </summary>
        private void Bind()
        {
            LinqCustomerDao customerDao = new LinqCustomerDao();

            GridViewCustomers.DataSource = customerDao.GetCustomers(SortExpression);
            GridViewCustomers.DataBind();
        }

        #region Sorting

        /// <summary>
        /// Handler Khi click vao button sort
        /// </summary>
        protected void GridViewCustomers_Sorting(object sender, GridViewSortEventArgs e)
        {
            SortDirection = (SortDirection == "ASC") ? "DESC" : "ASC";
            SortColumn = e.SortExpression;

            Bind();
        }

        /// <summary>
        /// Adds glyphs to gridview header according to sort order.
        /// Handler khi item duoc tao toi GridView
        /// </summary>
        protected void GridViewCustomers_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                AddGlyph(this.GridViewCustomers, e.Row);
            }
        }

        ///Handler khi item duoc bind toi GridView
        protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton linkButton = e.Row.Cells[5].Controls[0] as LinkButton;
                // Escape single quotes in Javascript.
                string company = DataBinder.Eval(e.Row.DataItem, "Company").ToString().Replace("'", "\\'");
                linkButton.Attributes.Add("onclick", "javascript:return " +
                "confirm('OK to delete \"" + company + "\"?')");
            }
        }

        #endregion

        /// <summary>
        /// Deletes selected customer from database.
        /// Handler khi user chon xoa..
        /// </summary>
        protected void GridViewCustomers_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var row = GridViewCustomers.Rows[e.RowIndex];
            int customerId = int.Parse(row.Cells[0].Text);

            LinqCustomerDao customerDao = new LinqCustomerDao();

            var customer = customerDao.GetCustomer(customerId);

            if (customer.Orders.Count > 0)
            {
                string customerName = row.Cells[1].Text;
                LabelError.Text = "Cannot delete " + customerName + " because they have existing orders!";
            }
            else
            {
                customerDao.DeleteCustomer(customer);

                Bind();
            }
        }
    }
}

2. DetailsView

        /// <summary>
        /// Executed only once. Used to place cursor in first editable field.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DetailsView_OnDataBound(object sender, EventArgs e)
        {
            if (DetailsViewCustomer.Rows.Count < 1) return;

            var row = DetailsViewCustomer.Rows[1];
            var textBox = row.Cells[1].Controls[0] as TextBox;
            textBox.Focus();
        }

GridView và DetailsView Design

1. GridView

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
     <ContentTemplate>
     <asp:GridView ID="GridViewCustomers" runat="server"
        DataKeyNames="CustomerId"
        AutoGenerateColumns="False" Width="600"
        AllowSorting="True"
        OnRowDataBound="GridView_RowDataBound"
        OnSorting="GridViewCustomers_Sorting"
        OnRowCreated="GridViewCustomers_RowCreated"
        OnRowDeleting="GridViewCustomers_RowDeleting" >
        <Columns>
    	  	<asp:BoundField HeaderText="Id" DataField="CustomerId" SortExpression="CustomerId" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="50" />
          <asp:BoundField HeaderText="Customer Name" DataField="Company" SortExpression="CompanyName" HeaderStyle-Width="220" />
			    <asp:BoundField HeaderText="City" DataField="City" SortExpression="City" HeaderStyle-Width="120" />
			    <asp:BoundField HeaderText="Country" DataField="Country" SortExpression="Country" HeaderStyle-Width="80" />
			    <asp:HyperLinkField HeaderText="Edit" DataNavigateUrlFields="CustomerId" DataNavigateUrlFormatString="~/admin/customers/{0}"
                Text="Edit" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Width="60" />
			    <asp:CommandField HeaderText="Delete" ButtonType="Link" ItemStyle-ForeColor="#006666"
			          ShowDeleteButton="True" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="70"  />
        </Columns>
    </asp:GridView>
   </ContentTemplate>
  </asp:UpdatePanel>

2. DetailsView

<asp:DetailsView ID="DetailsViewCustomer" runat="server"
         DataKeyNames="CustomerId"
         OnDataBound="DetailsView_OnDataBound"
         >
         <Fields>
           <asp:BoundField DataField="CustomerId" HeaderText="Id&nbsp;" InsertVisible="False" ReadOnly="True" ItemStyle-BackColor="#ffeecc"  ItemStyle-Font-Bold="True" />
           <asp:BoundField DataField="Company" HeaderText="Company&nbsp;" />
           <asp:BoundField DataField="City" HeaderText="City&nbsp;" />
           <asp:BoundField DataField="Country" HeaderText="Country&nbsp;" />
         </Fields>
         <FooterTemplate>
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <asp:Button ID="ButtonSave" Runat="server" Text=" Save " onclick="ButtonSave_Click"></asp:Button>
              &nbsp;&nbsp;<asp:Button ID="ButtonCancel" Runat="server" Text="Cancel" onclick="ButtonCancel_Click"></asp:Button>
         </FooterTemplate>
         <FooterStyle Height="40" BackColor="White" HorizontalAlign="Center" />
      </asp:DetailsView>