This is how you can add a template field in the code behind. You have to handle the WebDataGrid’s Init event, and in the Init event handler (WebDataGrid1_Init in the example) do the following
protected void WebDataGrid1_Init(object sender, EventArgs e)
{
TemplateDataField tempField = new TemplateDataField();
tempField.Key = "MyTempField";
tempField.Header.Text = "My Dynamic Checkbox";
CompiledBindableTemplateBuilder cbtb = new CompiledBindableTemplateBuilder(new BuildTemplateMethod(BuildCheck), null);
tempField.ItemTemplate = cbtb;
this.WebDataGrid1.Columns.Add(tempField);
}
void BuildCheck (Control ctl)
IParserAccessor acess = ctl;
CheckBox ch = new CheckBox();
ch.ID = "check2";
acess.AddParsedSubObject(ch);
Hi Dinesh,
Thanks. I have created the checkbox template column successfully.
How shall i trigger an event for this checkbox
or
how shall i get this checked value from code-behind?
I am googling this for past 2 weeks ... could you provide a solution as soon as possible.
Hi,
Suppose you have registered InitializeRow event handler you will be able to get the value of particular control inside the template column like this :
this.WebDataGrid1.InitializeRow += new InitializeRowEventHandler(WebDataGrid1_InitializeRow);
void WebDataGrid1_InitializeRow(object sender, RowEventArgs e)
LinkButton lb = (LinkButton)e.Row.Items.FindItemByKey("TemplateColumn").FindControl("LinkButton1");
I have successfully added a TemplateDataField to my WebDataGrid but now I have two problems.
1. Although I set the CommandName and CommandArgument I cannot get the ItemCommand event to fire. I get a postbask, but the ItemCommand event does not fire. Here is my code:
Sub BuildLinks(ByVal ctrl As Control) Dim access As IParserAccessor = ctrl Dim link As New LinkButton link.Text = "View" link.CommandArgument = "1" link.ID = "linkView" link.CommandName = "idEmployee" access.AddParsedSubObject(link)End Sub
//Here is the code to add the TemplateAddHandler ProjectedGrid.ItemCommand, AddressOf ProjectedGrid_ItemCommandDim templateField As New TemplateDataFieldtemplateField.Header.Text = "Action"templateField.Key = "KEY_ACTION"Dim TemplateItem As New CompiledBindableTemplateBuilder(New BuildTemplateMethod(AddressOf BuildLinks), Nothing)templateField.ItemTemplate = TemplateItemProjectedGrid.Columns.Add(templateField)
//In my InitializeRow event I haveDim actionLink As LinkButton = e.Row.Items.FindItemByKey("KEY_ACTION").FindControl("linkView")actionLink.CommandArgument = idEmployee
2. After the postback, the LinkButton is gone. The column is still there but the LinkButton is not.
Thoughts?
Nevermind. I moved the creating of the TemplateField to Init and that fixed both issues.
Mark