Filling with Dynamic Templatefield

Asked

Viewed 562 times

1

I dynamically create the templatefield to my gridview, when I get to gridview is rendered, I get the error that the "C" element does not exist as column names.

The code that error occurs is:

 dataValue = DataBinder.Eval(container.DataItem, _columnName);

The problem is that the ColumnName has the following name C.json and the DataBinder.Eval only reads to the point. This explains the error and read only to the "C".

How can I make you read the whole name, including the point?

  • You could put the exception you’re getting?

1 answer

0


Using the common form of DataBinder.Eval doesn’t really work for field names that contain ..

The alternative forms are:

((System.Data.DataRowView)Container.DataItem)["C.json"] 

or

DataBinder.GetPropertyValue(Container.DataItem, "C.json")

I personally find the second most elegant and simple.

That’s because DataBinder.Eval wait, in the second parameter, a expression to find the column name or property of a class. Thus, the point influences the resolution of the parameter.

See more details about the method DataBinder.Eval in the MSDN.

An example

THE HTML:

<asp:Repeater ID="ProductList" runat="server">
    <ItemTemplate>
        O id é: <%# DataBinder.Eval(Container.DataItem, "id") %> <br /> 
        Para o item: <%# DataBinder.Eval(Container.DataItem, "item") %> <br />
        Valor com ponto: <%# ((System.Data.DataRowView)Container.DataItem)["C.json"] %> <br />
        Outra forma: <%# DataBinder.GetPropertyValue(Container.DataItem, "C.json") %> <br />

        <br />
        <hr />
    </ItemTemplate>
</asp:Repeater>

C# no page_load to fill the:

protected void Page_Load(object sender, EventArgs e)
{
    var products = new DataTable();
    DataRow row;

    products.Columns.Add(new DataColumn() { ColumnName = "id" });
    products.Columns.Add(new DataColumn() { ColumnName = "item" });
    products.Columns.Add(new DataColumn() { ColumnName = "C.json"});

    for (int i = 0; i < 10; i++)
    {
        row = products.NewRow();
        row["id"] = i;
        row["item"] = "item " + i.ToString();
        row["C.json"] = "valor com . no nome";
        products.Rows.Add(row);
    }

    ProductList.DataSource = products;
    ProductList.DataBind();

}

The result:

Resultado de um dos itens do repeater

Browser other questions tagged

You are not signed in. Login or sign up in order to post.