读书人

怎么在自定义的继承自GridView的控件中

发布时间: 2011-12-28 22:45:21 作者: rapoo

如何在自定义的继承自GridView的控件中加入PagerTemplate行。
我想实现自定义分页,但不想每个页面都重复去写代码,因此决定扩展GridView空间,问题是如何动态创建PagerTemplate并加入到GridView中
主要是想实现不同的翻页方式。

我不想用现成的开发好的,未开源的控件。

[解决办法]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Saga.LivePortal.CustomControl
{
[ToolboxData( " <{0}:MyGridView runat=server> </{0}:MyGridView> ")]
public class MyGridView : System.Web.UI.WebControls.GridView, IPostBackEventHandler, IPostBackDataHandler
{
/// <summary>
///
/// </summary>
public MyGridView()
{
base.SelectedRowStyle.CssClass = "SelectedItem ";
base.RowStyle.CssClass = "GridItem ";
base.AlternatingRowStyle.CssClass = "GridAltItem ";
base.HeaderStyle.CssClass = "GridHeader ";
base.EditRowStyle.CssClass = "SelectedItem ";
base.RowStyle.Height = 20;
}
/// <summary>
///
/// </summary>
public delegate void OnReBindData();
/// <summary>
///
/// </summary>
public event OnReBindData ReBindData;
/// <summary>
/// const string stands for the navigate-to-first-page command sent from the grid
/// </summary>
private const string NAVIGATE_FIRST = "NAV_FIRST ";
/// <summary>
/// const string stands for the navigate-to-last-page command sent from the grid
/// </summary>
private const string NAVIGATE_LAST = "NAV_LAST ";
/// <summary>
/// const string stands for the navigate-to-previous-page command sent from the grid
/// </summary>
private const string NAVIGATE_PREVIOUS = "NAV_PREVIOUS ";
/// <summary>
/// const string stands for the navigate-to-next-page command sent from the grid
/// </summary>
private const string NAVIGATE_NEXT = "NAV_NEXT ";

private const string PAGESIZE_CONTROL = "PAGESIZE_CONTROL ";

private bool showPageNavigator = true;

public bool ShowPageNavigator
{
get
{
return showPageNavigator;
}
set
{
showPageNavigator = value;
}
}

#region Override GridView methods
public override void RenderControl(HtmlTextWriter writer)
{
try
{
writer.Write( " <table border=\ "0\ " class=\ "table_dark_inside\ " width=\ " " + this.Width.ToString() + "\ "

cellpadding=\ "0\ " cellspacing=\ "0\ "> ");
writer.Write( " <tr> <td align=\ "left\ " width=\ "100%\ "> ");
base.Render(writer);
writer.Write( " </td> </tr> ");
if (this.showPageNavigator)
{
writer.Write( " <tr> <td> ");
DrawPager(writer);


writer.Write( " </td> </tr> ");
}
writer.Write( " </table> ");
}
catch (Exception e)
{
System.Diagnostics.Debug.Write(e);
}
}

protected override void OnRowCreated(GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.Header)
{
// Call the GetSortColumnIndex helper method to determine
// the index of the column being sorted.
int sortColumnIndex = GetSortColumnIndex();

if (sortColumnIndex != -1)
{
// Call the AddSortImage helper method to add
// a sort direction image to the appropriate
// column header.
AddSortImage(sortColumnIndex, e.Row);
}
}
base.OnRowCreated(e);
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex);
}
}

/// <summary>
/// This is a helper method used to determine the index of the
/// column being sorted. If no column is being sorted, -1 is returned.
/// </summary>
/// <returns> </returns>
private int GetSortColumnIndex()
{
// Iterate through the Columns collection to determine the index
// of the column being sorted.
try
{
foreach (DataControlField field in this.Columns)
{
// judge the current sort column ,if first create ,then return columns[0]
if ((field.SortExpression == this.SortExpression) && (field.SortExpression != " "))
{
return this.Columns.IndexOf(field);
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex);
}
return -1;
}

/// <summary>
/// This is a helper method used to add a sort direction
/// image to the header of the column being sorted.
/// </summary>
/// <param name= "columnIndex "> </param>
/// <param name= "headerRow "> </param>
private void AddSortImage(int columnIndex, GridViewRow headerRow)
{
try
{
// Create the sorting image based on the sort direction.
Image sortImage = new Image();
if (this.SortDirection == SortDirection.Ascending)
{
sortImage.ImageUrl = "~/Images/LivePortal/up.gif ";
sortImage.AlternateText = "Ascending Order ";
}
else
{
sortImage.ImageUrl = "~/Images/LivePortal/down.gif ";
sortImage.AlternateText = "Descending Order ";
}
// Add the image to the appropriate header cell.
headerRow.Cells[columnIndex].Controls.Add(sortImage);
}
catch (Exception e)


{
System.Diagnostics.Debug.Write(e);
}
}

protected override void OnInit(EventArgs e)
{
try
{
this.PagerSettings.Visible = false;
this.AutoGenerateColumns = false;
base.OnInit(e);
RegisterControlAndScript();
Page.RegisterRequiresPostBack(this);
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex);
}
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}


#endregion

读书人网 >asp.net

热点推荐