I have a feeling this is a simple error, but I can't for the life of
me figure it out.
I am writing a custom webcontrol which has an internal DataGrid.
Everything works fine, except the pager links have no postback
javascript attached. In other words, instead of seeing something like
this:
<span>1</span>
<a href="javascript:__doPostBack('dgWPL$ctl14$ctl01','')">2</a>
<a href="javascript:__doPostBack('dgWPL$ctl14$ctl02','')">3</a>
<a href="javascript:__doPostBack('dgWPL$ctl14$ctl03','')">4</a>
<a href="javascript:__doPostBack('dgWPL$ctl14$ctl04','')">5</a> 
<a href="javascript:__doPostBack('dgWPL$ctl14$ctl05','')">6</a>
which is what I get when I use a datagrid directly on my testing page,
I see this:
<span>1</span>
<a>2</a>
<a>3</a>
<a>4</a>
<a>5</a>
<a>6</a>
Let me lay out my code for you.
public class CustomGridControl: WebControl
{
private DataGrid dg;
private DocumentCollection docs; // Implements IDataSource
public CustomGridControl()
{
dg= new DataGrid();
dg.AutoGenerateColumns = false;
dg.Width = Unit.Pixel(580);
dg.AllowPaging = true;
dg.PageSize = 10;
dg.AlternatingItemStyle.BackColor = Color.FromArgb(224,
224, 224);
dg.ItemStyle.BackColor = Color.FromArgb(240, 240, 240);
dg.PagerStyle.Mode = PagerMode.NumericPages;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
TemplateColumn temp = new TemplateColumn();
temp.ItemTemplate = new
CustomTemplateColumn(DataControlRowType.DataRow, "Documents");
// CustomTemplateColumn handles my custom databind
// by implementing ITemplate
dg.ID = "dg.1";
dg.Columns.Add(temp);
dg.PageIndexChanged += new
DataGridPageChangedEventHandler(dg_PageIndexChanged);
if (!Page.IsPostBack && docs != null)
{
dg.DataSource = docs;
dg.DataBind();
}
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
dg.RenderControl(writer);
}
void dg_PageIndexChanged(object source,
DataGridPageChangedEventArgs e)
{
dg.CurrentPageIndex = e.NewPageIndex;
dg.DataSource = docs;
dg.DataBind();
}
}
And that's pretty much it. I declare an instance of one of these on my
aspx page, feed it a DocumentCollection in the code-behind, and run.
The data renders perfectly, but the pager links are empty and don't
work.
Any thoughts? Thanks in advance,
Jordan Weber-Flink
Josiwe - 22 Jun 2007 21:57 GMT
<snip>
For those who read this later, I found the solution. Because I was
rendering the grid directly, it was not part of the page's control
hierarchy. I removed the render overload and added the following code
to the OnLoad:
this.Controls.Add(dg);
And the pager worked. This introduced some other problems with event
handling, but these are easily solved with a few session variables.
Good Luck!