Adding a button to a datagrid

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

I think I must be going crazy. All I want to do is add a button to the
bottom of a datagrid which will later run some code for a web based
project. I've created a custom control that inherits from the datagrid
and have added just a single line of code thus;

protected override void Render(HtmlTextWriter output)
{

output.Write(new System.Web.UI.WebControls.Button());

base.Render(output);
}
All looks fine at design time but at runtime I'm getting a cast
exception - what am I missing? Many thanks if you can point out where
I'm going wrong.
 
Hi,

You don't need to inherit from datagrid for just adding a button to it.

You can just add a button column or a temlate column from all the templates
supported by datagrid.


Hope this helps.

Reshma
 
Hi,

You do not need to create a new class, just use the Page PreRender event
and add a control to the datagrid.Controls collection

One question though, if you know for sure it will always go there, why not
declare it in the page?
Even if sometimes it's not shown it's easier to declare it alone and use
Visible = false;

Btw, you are getting error below cause Write expect a string, not a Button
instance

Cheers,
 
Thank you for your helpful suggestions. However I do want the button to
be part of the datagrid, the reason being is that it will interact with
the data in the grid. Furthermore I want to be able to expose a property
in the designer that allows a developer to choose whether the button is
visible.

Regards,

Chris
 
Hi,

Are you designing a control?

From your first post that is not clear

I still think that including the textbox in the grid is not good idea, what
interaction are you planning between the data & the textbox?

cheers,
 
Back
Top