I am writing an application that will display database queries. There are different tables and I would like to be able to choose between them with radio buttons. This is currently working properly, but now I am trying to figure out how to format the data repeater based on which table will be displayed (there are different numbers of columns, in a different order, etc.) I got the header to format successfully since it is just basic HTML, but the item template requires using asp inline code, which I believe is causing the problem.
relevant code from code-behind:
protected string showDataTableItemTemplate()
{
string itemTemplateText = string.Empty;
if (tableSelection == "parts")
{
itemTemplateText += "<td><%# DataBinder.Eval(Container,\"DataItem.PartNumber\") %></td>";
itemTemplateText += "<td><%# DataBinder.Eval(Container,\"DataItem.Description\") %></td>";
}
else if (tableSelection == "package")
{
itemTemplateText += "<td><%# DataBinder.Eval(Container,\"DataItem.PackageNumber\") %></td>";
itemTemplateText += "<td><%# DataBinder.Eval(Container,\"DataItem.Description\") %></td>";
}
else if (tableSelection == "product")
{
itemTemplateText += "<td><%# DataBinder.Eval(Container,\"DataItem.ProductNumber\") %></td>";
itemTemplateText += "<td><%# DataBinder.Eval(Container,\"DataItem.Description\") %></td>";
}
return itemTemplateText;
}
Then I just have the literal defined like this in the aspx:
<asp:Literal runat="server" Text='<%#showDataTableItemTemplate() %>'></asp:Literal>
Is there another way to do this?
EDIT: I now have the following:
protected void DataRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
LiteralControl itemTemplateLiteral = new LiteralControl();
itemTemplateLiteral = (LiteralControl)FindControl("itemTemplate");
string itemTemplateText = string.Empty;
if (tableSelection == "parts")
{
itemTemplateText += "<td><%# DataBinder.Eval(Container,\"DataItem.PartNumber\") %></td>";
itemTemplateText += "<td><%# DataBinder.Eval(Container,\"DataItem.Description\") %></td>";
}
else if (tableSelection == "package")
{
itemTemplateText += "<td><%# DataBinder.Eval(Container,\"DataItem.PackageNumber\") %></td>";
itemTemplateText += "<td><%# DataBinder.Eval(Container,\"DataItem.Description\") %></td>";
}
else if (tableSelection == "product")
{
itemTemplateText += "<td><%# DataBinder.Eval(Container,\"DataItem.ProductNumber\") %></td>";
itemTemplateText += "<td><%# DataBinder.Eval(Container,\"DataItem.Description\") %></td>";
}
itemTemplateLiteral.Text = itemTemplateText;
}
The compiler keeps telling me that there is an Object reference not set to an instance of an object. error. Also, Am I correct in trying to set the Text property still? (Wasn’t that what I was trying to avoid by using this method?)
You should have an event handler for the
OnItemDataBoundevent of the template control and in it useFindControlto locate theLiteralcontrol and assign values to it using thee.Item.DataItem.What your current code is doing is simply assigning strings to the literal – these will not be evaluated as part of page markup because they are not part of the markup.