Молчун
Зарегистрирован: 11 February 2008
Сообщения: 35
Примеры кода: 0
|
Re: UserControl на основе GridView |
10 March 2008 14:26 |
|
|
|
|
Сам отвечаю на поставленный мной же вопрос: как я и думал, делать WebControl в этом случае не самое лучшее решение; правильное решение --- это создать свой класс от GridView и создать в нём свой "PageTemplate".
Вот пример класса нового GridView с изменнённой "PageTemplate". Всё оказалось не так сложно как я думал, но почему-то в Интернете я не нашёл ниодно конкретного примера.
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
[assembly: TagPrefix("MapServer.Controles", "gvGridView")]
namespace MapServer.Controles
{
public class gvGridView : GridView
{
public gvGridView()
{
base.PagerStyle.HorizontalAlign = HorizontalAlign.Center;
base.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
base.HeaderStyle.Height = 20;
base.PageSize = 20;
}
protected override void OnDataBound(EventArgs e)
{
base.OnDataBinding(e);
try
{
GridView gvGrid = this;
GridViewRow gvrPagerRow = gvGrid.BottomPagerRow;
Label lblPagina = (Label)gvrPagerRow.Cells[0].FindControl("lblPagina");
Label lblTotal = (Label)gvrPagerRow.Cells[0].FindControl("lblTotal");
LinkButton lbtnPrimero = (LinkButton)gvrPagerRow.Cells[0].FindControl("lbtnPrimero");
Label lblPrimero = (Label)gvrPagerRow.Cells[0].FindControl("lblPrimero");
LinkButton lbtnAnterior = (LinkButton)gvrPagerRow.Cells[0].FindControl("lbtnAnterior");
Label lblAnterior = (Label)gvrPagerRow.Cells[0].FindControl("lblAnterior");
LinkButton lbtnSiguiente = (LinkButton)gvrPagerRow.Cells[0].FindControl("lbtnSiguiente");
Label lblSiguiente = (Label)gvrPagerRow.Cells[0].FindControl("lblSiguiente");
LinkButton lbtnUltimo = (LinkButton)gvrPagerRow.Cells[0].FindControl("lbtnUltimo");
Label lblUltimo = (Label)gvrPagerRow.Cells[0].FindControl("lblUltimo");
if ((lblPagina != null) && (lblTotal != null))
{
lblPagina.Text = (gvGrid.PageIndex + 1).ToString();
lblTotal.Text = gvGrid.PageCount.ToString();
}
if ((lbtnPrimero != null) && (lblPrimero != null) && (lbtnAnterior != null) && (lblAnterior != null))
{
if (gvGrid.PageIndex == 0)
{
lblAnterior.Visible = true;
lbtnAnterior.Visible = false;
lblPrimero.Visible = true;
lbtnPrimero.Visible = false;
}
else
{
lblAnterior.Visible = false;
lbtnAnterior.Visible = true;
lblPrimero.Visible = false;
lbtnPrimero.Visible = true;
}
}
if ((lbtnSiguiente != null) && (lblSiguiente != null) && (lbtnUltimo != null) && (lblUltimo != null))
{
if (gvGrid.PageCount == gvGrid.PageIndex + 1)
{
lbtnSiguiente.Visible = false;
lblSiguiente.Visible = true;
lbtnUltimo.Visible = false;
lblUltimo.Visible = true;
}
else
{
lbtnSiguiente.Visible = true;
lblSiguiente.Visible = false;
lbtnUltimo.Visible = true;
lblUltimo.Visible = false;
}
}
}
catch
{
base.OnDataBound(e);
}
}
protected override void OnRowDataBound(GridViewRowEventArgs e)
{
base.OnRowDataBound(e);
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.className='InteriorTablaListaSelected';");
e.Row.Attributes.Add("onmouseout", "this.className='InteriorTablaLista'");
}
}
protected override void InitializePager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
{
TableCell tbclCellPeger = new TableCell();
if (columnSpan > 1)
tbclCellPeger.ColumnSpan = columnSpan;
tbclCellPeger.Controls.Add(PagerControl());
if (PagerTemplate != null)
PagerTemplate.InstantiateIn(tbclCellPeger);
row.Cells.Add(tbclCellPeger);
}
private Table PagerControl()
{
Table tbPager = new Table();
try
{
tbPager.ID = "tblPager";
TableRow tbrwRow = new TableRow();
TableCell tbclCell = new TableCell();
tbclCell.HorizontalAlign = HorizontalAlign.Right;
LinkButton lbtnPrimero = new LinkButton();
lbtnPrimero.ID = "lbtnPrimero";
lbtnPrimero.CommandArgument = "First";
lbtnPrimero.CommandName = "Page";
lbtnPrimero.Text = "<< ";
Label lblPrimero = new Label();
lblPrimero.ID = "lblPrimero";
lblPrimero.Text = "<< ";
tbclCell.Controls.Add(lbtnPrimero);
tbclCell.Controls.Add(lblPrimero);
tbrwRow.Cells.Add(tbclCell);
tbclCell = new TableCell();
tbclCell.HorizontalAlign = HorizontalAlign.Right;
LinkButton lbtnAnterior = new LinkButton();
lbtnAnterior.ID = "lbtnAnterior";
lbtnAnterior.CommandArgument = "Prev";
lbtnAnterior.CommandName = "Page";
lbtnAnterior.Text = "< ";
Label lblAnterior = new Label();
lblAnterior.ID = "lblAnterior";
lblAnterior.Text = "< ";
tbclCell.Controls.Add(lbtnAnterior);
tbclCell.Controls.Add(lblAnterior);
tbrwRow.Cells.Add(tbclCell);
tbclCell = new TableCell();
tbclCell.HorizontalAlign = HorizontalAlign.Right;
Label lblPagina = new Label();
lblPagina.ID = "lblPagina";
lblPagina.Text = "1";
tbclCell.Controls.Add(lblPagina);
tbrwRow.Cells.Add(tbclCell);
tbclCell = new TableCell();
tbclCell.HorizontalAlign = HorizontalAlign.Center;
tbclCell.Text = "de";
tbrwRow.Cells.Add(tbclCell);
tbclCell = new TableCell();
tbclCell.HorizontalAlign = HorizontalAlign.Right;
Label lblTotal = new Label();
lblTotal.ID = "lblTotal";
lblTotal.Text = "5";
tbclCell.Controls.Add(lblTotal);
tbrwRow.Cells.Add(tbclCell);
tbclCell = new TableCell();
tbclCell.HorizontalAlign = HorizontalAlign.Right;
LinkButton lbtnSiguiente = new LinkButton();
lbtnSiguiente.ID = "lbtnSiguiente";
lbtnSiguiente.CommandArgument = "Next";
lbtnSiguiente.CommandName = "Page";
lbtnSiguiente.Text = " >";
Label lblSiguiente = new Label();
lblSiguiente.ID = "lblSiguiente";
lblSiguiente.Text = " >";
tbclCell.Controls.Add(lblSiguiente);
tbclCell.Controls.Add(lbtnSiguiente);
tbrwRow.Cells.Add(tbclCell);
tbclCell = new TableCell();
tbclCell.HorizontalAlign = HorizontalAlign.Right;
LinkButton lbtnUltimo = new LinkButton();
lbtnUltimo.ID = "lbtnUltimo";
lbtnUltimo.CommandArgument = "Next";
lbtnUltimo.CommandName = "Page";
lbtnUltimo.Text = " >>";
Label lblUltimo = new Label();
lblUltimo.ID = "lblUltimo";
lblUltimo.Text = " >>";
tbclCell.Controls.Add(lblUltimo);
tbclCell.Controls.Add(lbtnUltimo);
tbrwRow.Cells.Add(tbclCell);
tbPager.Rows.Add(tbrwRow);
}
catch
{
tbPager = null;
}
return tbPager;
}
}
}
|
|