I've turned on paging in my WebGrid. I've got a templated column in my grid that is used to display a hyperlink. When move to the next page, I get the following error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
HTML: <igtbl:TemplatedColumn BaseColumnName="ID" Key="ID"> <Header Caption="ID"> </Header> <CellTemplate> <a href="WorkOrder.aspx?WorkOrderID=<%# Eval("ID") %>"><%# Eval("ID") %></a> </CellTemplate> </igtbl:TemplatedColumn>
How would one have a template column and paging work at the same time?
Rick
I have general problem with the TemplatedColumns.
Here is my markup for UltraWebGrid, I want to bind the value for the Name key into a Label and it doesn't work, it doesn't display anything.
<ig:UltraWebGrid ID="UltraWebGrid1" runat="server" DataSourceID="xmlDSAnimals" Height="200px" Width="325px"> <Bands> <ig:UltraGridBand AddButtonCaption="Animal" BaseTableName="Animal" Key="Animal"> <Columns> <ig:TemplatedColumn IsBound="True" BaseColumnName="Name" Key="Name"> <Header Caption="Name" /> <CellTemplate> <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name", "Your name: {0}") %>'></asp:Label> </CellTemplate> </ig:TemplatedColumn> </Columns> </ig:UltraGridBand> </Bands> </ig:UltraWebGrid>
Hello Iva,
Thanks for writing. I guess, it really depends on the structure of your DataSource. Judging from your aspx markup, you are binding to an XmlDataSource. Could you please paste a small sample of your Xml datasource here, maybe this will provide some clues? Also, can you please try binding a standard asp:GridView control with templated columns to the same datasource and see how it goes?
I have tried something very simple (declarative flat AccessDataSource control + templated columns) and everything worked fine in my case - here is my setup:
<igtbl:UltraWebGrid ID="UltraWebGrid1" runat="server" DataSourceID="AccessDataSource1" Height="200px" Width="325px"> <Bands> <igtbl:UltraGridBand> <Columns> <igtbl:TemplatedColumn> <CellTemplate> <asp:Label ID="lblName" runat="server" Text='<%# Eval("ProductName", "Your name: {0}") %>'></asp:Label> </CellTemplate> </igtbl:TemplatedColumn> ... </Columns>...
I try to bind the XmlDataSource to a standart asp:GridView and it's working corect.
I made a test project to try grid binding, it isn't my real project, so I also can put the whole project code.
This is the xml file:
<?xml version="1.0" encoding="utf-8" ?><Animals> <Animal Name="Doc" Color="Gray" /> <Animal Name="Cat" Color="Brown" /> <Animal Name="Bird" Color="Blue" /> <Animal Name="Crocodile" Color="Green" /></Animals>
I solve my problem with the binding with this code:
<ig:TemplatedColumn IsBound="True" BaseColumnName="Name" Key="Name"> <CellTemplate> <asp:Label runat="server" ID="lblName" Text='<%# Container.Cell.Text %>'></asp:Label> </CellTemplate> </ig:TemplatedColumn>
You can also use
<%# Container.Cell.Row.Cells.FromKey("column2").Value >
to get the value from other column.
Oh, great find, thanks for sharing. So I guess, UltraWebGrid behaves a little bit differently than the asp:GridView control when using templated columns, so Container.Cell is a good starting point, plus you get all the extra functionality for Row, etc.
By the way, if you are not completely sure which properties/objects are available in your template databinding expressions, you can map values using server-side methods, for example:
<igtbl:templatedcolumn isbound="True" basecolumnname="Name" key="Name"> <CellTemplate> <asp:Label runat="server" ID="Label1" Text='<%# MapStuff(Container) %>'></asp:Label> </CellTemplate></igtbl:templatedcolumn>
public string MapStuff(Infragistics.WebUI.UltraWebGrid.CellItem cellItem) { // return string depending on custom logic }
This way, you can use the VS 2005/2008 Autocomplete functionality and see all available information for the Container using the VS debugger.