Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Controls / June 2007

Tip: Looking for answers? Try searching our database.

Pager Error when using DataGrid.RenderControl inside custom control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Josiwe - 21 Jun 2007 23:29 GMT
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>&nbsp;
<a href="javascript:__doPostBack('dgWPL$ctl14$ctl01','')">2</a>&nbsp;
<a href="javascript:__doPostBack('dgWPL$ctl14$ctl02','')">3</a>&nbsp;
<a href="javascript:__doPostBack('dgWPL$ctl14$ctl03','')">4</a>&nbsp;
<a href="javascript:__doPostBack('dgWPL$ctl14$ctl04','')">5</a>&nbsp
<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>&nbsp;
<a>2</a>&nbsp;
<a>3</a>&nbsp;
<a>4</a>&nbsp;
<a>5</a>&nbsp;
<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!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.