Hi,
I have been try to programmatically add templates to a WebDataGrid. This is described here:
http://help.infragistics.com/NetAdvantage/ASPNET/2011.1/CLR4.0/?page=WebDataGrid_Using_the_Template_Collection.html
I was successful with this. However, my next step was to set up some template control bindings...programmatically. The help documentation describes how to add template control bindings the Data Bindings Editor, here:
http://help.infragistics.com/NetAdvantage/ASPNET/2011.1/CLR4.0/?page=WebDataGrid_Template_Control_Bindings.html
The resulting ASP.NET markup looks something something like this:
...
</Columns> <Templates> ... <ig:ItemTemplate ID="WebDataGrid1Template2" runat="server" TemplateID="Template2"> <Template> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("name", "{0}") %>'></asp:TextBox> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("priority") %>'>HyperLink</asp:HyperLink> </Template> </ig:ItemTemplate> </Templates> </ig:WebDataGrid>
I can understand how to do this win markup However, I cannot see how I would add these template control bindings programmatically. Is there any help documentation on this? Are there any samples that demonstrate how to do this?
Issue:
1) I have created a TemplateDataField with Textbox.
2) I am trying to findcontrol to get the value of textbox
3) It ways NULL, ItemTemplate is not retained at the POSTBACK.
Steps to reproduce:
1) Attached page has two grid (top) - dynamically created columns
2) Test1 button - print NULLs on findcontrol
2) 2nd grid is static columns through ASPX.
3) Test2 button - print VALUEs on findcontrol
Please help fixing the grid1/Test1 button and send me back the ASPX and ASPX.CS
Thanks in advance.
It worked for me too!
I have just one complaint, I had to re-write the code in VB. But in the end, it was worth the effort!
And I guess, you guys could put such sample codes out there in your documentation, because inevitable, people are going to need such code.
Thanks a lot Tsvetelina Georgieva, I hope you will take my comments in the right way!
Hi Tsvetelina,
Thanks for your suggestions. I have been able to make this work now.
In hindsight, I can see that there were 2 areas where I did not understand the API for implementing an ITemplate together with the WebDataGrid:
1. Misunderstanding the meaning of the ITemplate.InstantiateIn() method:
I read this as meaning "instantiate a template...in order to build the control structure".
Now, I can see that it means "instantiate AN INSTANCE OF the template...in order to build the control structure, and populate with grid cell values"
2. Not understanding which types to cast to
I was not aware of the casting I needed to to do, such as: to TemplateContainer, to DataRowView, or to GridRecordItem. Now that I understand these better, I can access all of the information that I need.
Thanks very much for your help resolving this.
Joel
Hello Joel,
Thank you for posting in our forums.
I guess you want to achieve the below but in the code behind:
<ig:TemplateDataField Key="Message">
<ItemTemplate>
<asp:Image ID="Image1" ImageUrl='<%# DataBinder.Eval(((Infragistics.Web.UI.TemplateContainer)Container).DataItem, "Message") %>'
runat="server" />
</ItemTemplate>
<Header Text="Message" />
</ig:TemplateDataField>
It depend on the datasource that you are using , but is it DataTable for exmple the approach is the following:
public void InstantiateIn(Control container)
{
CheckBox chkBox = new CheckBox();
chkBox.ID = "TemplateCheckBox";
// Get the cell value from the DataItem
chkBox.Checked = (bool)((DataRowView)((TemplateContainer)container).DataItem)["BoolColumn"];
HyperLink link = new HyperLink();
link.ID = "TemplateHyperLink";
string dataValue = (string)((DataRowView)((TemplateContainer)container).DataItem)["Data"];
link.NavigateUrl = string.Format("http://www.google.bg/search?q={0}", dataValue);
link.Text = (string)((DataRowView)((TemplateContainer)container).DataItem)["Data"];
container.Controls.Add(link);
container.Controls.Add(chkBox);
}
Please refer to the sample code attached and let me know if you need further assistance.