Need help for a simple composite control

E

eric dugal

Hi!!

I just want to create a composite control derived from a datagrid and a one
or more button on top of the grid in a table.

I already tried to put my button on the createchildrenControls method :

Controls.Clear();

base.CreateChildControls(); // again, this will call Controls.Clear() so do
it first.


btn = new LinkButton();


btn.ID = "btn1";

btn.Text = "Export";

btn.Click += new System.EventHandler

(this.btn_Click);

this.Controls.Add(btn);



and in the render method , i wrote :


base.rendercontrols();


but this way, the button is at the button of the grid, and the auto-format
functionnality of the grid doesn't work anymore.

Thanks, for any help.
 
C

Chris Austin

Hi Eric,

I don't think you need to derive from DataGrid. Instead, for a composite
control just derive from WebControl and create an override for the
CreateChildControls method and implement something like the following:
protected override void CreateChildControls()
{
base.CreateChildControls ();
DataGrid myDataGrid = new DataGrid();

HtmlTableRow row1 = new HtmlTableRow();
HtmlTableCell cell1 = new HtmlTableCell();
row1.Cells.Add(cell1);
Button button1 = new Button();
button1.Text = "My Button";
cell1.Controls.Add(button1);

HtmlTableRow row2 = new HtmlTableRow();
HtmlTableCell cell2 = new HtmlTableCell();
row2.Cells.Add(cell2);
cell2.Controls.Add(myDataGrid);

HtmlTable table = new HtmlTable();
table.Rows.Add(row1);
table.Rows.Add(row2);

Controls.Add(table);

//This is just for test data
ArrayList dataArray = new ArrayList();
dataArray.Add("Test 1");
dataArray.Add("Test 2");
dataArray.Add("Test 3");
dataArray.Add("Test 4");
dataArray.Add("Test 5");

myDataGrid.DataSource = dataArray;
myDataGrid.DataBind();

}


HTH

-Chris
http://weblogs.austinspad.com/caustin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top