Re: CustomDataGrid Child controls event problem - weird!?!

From: YunusEmre (emrenews-maillist_at_yahoo.com)
Date: 12/21/04


Date: Tue, 21 Dec 2004 15:57:59 +0200

after some more work on the code I came to realize that the call to the
DataBind() method causes the controls collection of the grid to be cleared.
And controls that I put there are also gone. If they are to be cleared
everytime DataBind() is called, how can I add any other controls that I need
on the control?

"YunusEmre" <emrenews-maillist@yahoo.com> wrote in message
news:utXf66u5EHA.2452@TK2MSFTNGP14.phx.gbl...
> Hi,
>
> I am trying to build a Custom Web Control that extends the DataGrid
> control but I am having problems with the events of the child controls. As
> you can see in the following code I have four ImageButtons that I am
> willing to add to the grid. They will function as page navigators (First,
> Previous, Next, Last).
>
> I put this control on an aspx page. Set the properties of the control to
> allow paging, give it the image URLs for the page navigation buttons,
> write the code to bind data and run. Sth weird happens. The aspx page uses
> GridLayout. MyGrid is in the middle of the page. When the application
> starts the grid is in place, the data is on the grid but the buttons are
> not there. Even if I force them to render by overriding the "render"
> method they don't respond. After I click on another button on the page
> (which has no relation with the grid) or one the buttons that I forced to
> render in the overridden render method, my page navigation buttons appear
> at the beginning of the page, at the upper left corner of the page. I
> click one of those on the corner or one of those that are rendered by me,
> it does its stuff and they disappear again.
>
> I have read many posts on child controls not firing events but I guess
> this one is weird.
>
> 1) How can I get the buttons running everytime on the page
>
> 2) How can I place them just above the grid.
>
> Here is the code for my customwebcontrol
>
> ================================================
>
> using System;
>
> using System.Data;
>
> using System.Drawing;
>
> using System.IO;
>
> using System.Web.UI;
>
> using System.Web.UI.WebControls;
>
> using System.ComponentModel;
>
>
>
> namespace MyGridPrj
>
> {
>
> [ToolboxData("<{0}:MyGrid runat=server></{0}:MyGrid>"),
>
> ToolboxBitmap(@"D:\MyGrid\MyGrid.bmp")]
>
> public class MyGrid : System.Web.UI.WebControls.DataGrid,
> System.Web.UI.INamingContainer
>
> {
>
>
> protected override void CreateChildControls()
>
> {
>
> this.Controls.Clear();
>
> base.CreateChildControls ();
>
> firstButton = new ImageButton();
>
> firstButton.CommandName = "First";
>
> firstButton.ImageUrl = firstButtonURL;
>
> firstButton.Command += new CommandEventHandler(OnPageButton_Command);
>
> prevButton = new ImageButton();
>
> prevButton.CommandName = "Prev";
>
> prevButton.ImageUrl = prevButtonURL;
>
> prevButton.Command += new CommandEventHandler(OnPageButton_Command);
>
> nextButton = new ImageButton();
>
> nextButton.CommandName = "Next";
>
> nextButton.ImageUrl = nextButtonURL;
>
> nextButton.Command += new CommandEventHandler(OnPageButton_Command);
>
> lastButton = new ImageButton();
>
> lastButton.CommandName = "Last";
>
> lastButton.ImageUrl = lastButtonURL;
>
> lastButton.Command += new CommandEventHandler(this.OnPageButton_Command);
>
> this.Controls.Add(firstButton);
>
> this.Controls.Add(prevButton);
>
> this.Controls.Add(nextButton);
>
> this.Controls.Add(lastButton);
>
> }
>
> // I have declared this dataSourceTable thing because I want to force the
> user of the component to bind a dataTable to the grid, so I manually bind
> it to the DataSource property of the base DataGrid class and so I can use
> a DataView object to sort,filter ans do stuff like that on the grid. But
> that is a future concern... First I have to get these buttons running.
>
> private static DataTable dataSourceTable;
>
> public DataTable DataSourceTable{
>
> get
>
> {
>
> return dataSourceTable;
>
> }
>
> set
>
> {
>
> dataSourceTable = value;
>
> this.DataSource = dataSourceTable;
>
> }
>
> }
>
>
>
> #region Page Navigation Button Declarations
>
> private ImageButton firstButton;
>
> private string firstButtonURL;
>
> [Bindable(true), Category("Paging"), Description("URL of the image to show
> as a first page button")]
>
> public string FirstButtonURL {
>
> get{ return this.firstButtonURL; }
>
> set{ this.firstButtonURL = value; }
>
> }
>
> private ImageButton prevButton;
>
> private string prevButtonURL;
>
> [Bindable(true), Category("Paging"), Description("URL of the image to show
> as a previous page button")]
>
> public string PrevButtonURL {
>
> get{ return this.prevButtonURL; }
>
> set{ this.prevButtonURL = value; }
>
> }
>
> private ImageButton nextButton;
>
> private string nextButtonURL;
>
> [Bindable(true), Category("Paging"), Description("URL of the image to show
> as a next page button")]
>
> public string NextButtonURL {
>
> get{ return this.nextButtonURL; }
>
> set{ this.nextButtonURL = value; }
>
> }
>
> private ImageButton lastButton;
>
> private string lastButtonURL;
>
> [Bindable(true), Category("Paging"), Description("URL of the image to show
> as a last page button")]
>
> public string LastButtonURL {
>
> get{ return this.lastButtonURL; }
>
> set{ this.lastButtonURL = value; }
>
> }
>
> #endregion
>
>
> protected void OnPageButton_Command(object source,
> System.Web.UI.WebControls.CommandEventArgs e)
>
> {
>
> switch (e.CommandName) {
>
> case "First" : { this.CurrentPageIndex = 0;
>
> break;}
>
> case "Prev" : { if (this.CurrentPageIndex>0) this.CurrentPageIndex =
> this.CurrentPageIndex-1;
>
> break;}
>
> case "Next" : { if (this.CurrentPageIndex<this.PageCount-1)
> this.CurrentPageIndex = this.CurrentPageIndex+1;
>
> break;}
>
> case "Last" : { this.CurrentPageIndex = this.PageCount-1;
>
> break;}
>
> }
>
> DataSource = dataSourceTable;
>
> DataBind();
>
> }
>
> }
>
>
>
> ================================================
>
>