Im having problem on having onitemdatabound

1 Answer 28 Views
Button Grid ImageButton
Edward
Top achievements
Rank 1
Edward asked on 29 Sep 2021, 03:00 PM

the problem is that i declared the function of onitemdatabound but theres an error that says it cant find the cell bound  

for example i have a telerik: gridtemplatecolumn with unique name action and meta resourcekey gtcaction 

then i have an item template where i have an imagebutton with present the id

 

 

1 Answer, 1 is accepted

Sort by
0
Vessy
Telerik team
answered on 01 Oct 2021, 02:21 PM

Hi Edward,

You can see how to access a control inside a template successfully in the following section of our documentation:

https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/accessing-values-and-controls/server-side/accessing-controls#accessing-controls-in-template-column

For example:

        <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" CellSpacing="0"
            GridLines="None" Width="800px" OnNeedDataSource="RadGrid1_NeedDataSource"
            OnItemDataBound="RadGrid1_ItemDataBound"
            AllowSorting="true">
            <MasterTableView AutoGenerateColumns="False" DataKeyNames="OrderID">
                <Columns>
                    <telerik:GridTemplateColumn UniqueName="gtcaction">
                        <ItemTemplate>
                            <telerik:RadImageButton ID="ImageButton1" runat="server" Text="Test"></telerik:RadImageButton>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridBoundColumn DataField="OrderID" DataType="System.Int32"
                        FilterControlAltText="Filter OrderID column" HeaderText="OrderID"
                        ReadOnly="True" SortExpression="OrderID" UniqueName="OrderID">
                    </telerik:GridBoundColumn>
                    <telerik:GridDateTimeColumn DataField="OrderDate" DataType="System.DateTime"
                        FilterControlAltText="Filter OrderDate column" HeaderText="OrderDate"
                        SortExpression="OrderDate" UniqueName="OrderDate">
                    </telerik:GridDateTimeColumn>
                    <telerik:GridNumericColumn DataField="Freight" DataType="System.Decimal"
                        FilterControlAltText="Filter Freight column" HeaderText="Freight"
                        SortExpression="Freight" UniqueName="Freight">
                    </telerik:GridNumericColumn>
                    <telerik:GridBoundColumn DataField="ShipName"
                        FilterControlAltText="Filter ShipName column" HeaderText="ShipName"
                        SortExpression="ShipName" UniqueName="ShipName">
                    </telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn DataField="ShipCountry"
                        FilterControlAltText="Filter ShipCountry column" HeaderText="ShipCountry"
                        SortExpression="ShipCountry" UniqueName="ShipCountry">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("ShipCountry") %>'></asp:Label>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
                <SortExpressions>
                    <telerik:GridSortExpression FieldName="Freight" SortOrder="Descending" />
                </SortExpressions>
            </MasterTableView>
        </telerik:RadGrid>

    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        (sender as RadGrid).DataSource = GetGridSource();
    }

    private DataTable GetGridSource()
    {
        DataTable dataTable = new DataTable();

        dataTable.Columns.Add(new DataColumn("OrderID", typeof(int)));
        dataTable.Columns.Add(new DataColumn("OrderDate", typeof(DateTime)));
        dataTable.Columns.Add(new DataColumn("Freight", typeof(decimal)));
        dataTable.Columns.Add(new DataColumn("ShipName", typeof(string)));
        dataTable.Columns.Add(new DataColumn("ShipCountry", typeof(string)));

        dataTable.PrimaryKey = new DataColumn[] { dataTable.Columns["OrderID"] };

        for (int i = 0; i < 70; i++)
        {
            // or use the following one-liner with the following arguments
            // OrderID, OrderDate, Freight, ShipName, ShipCountry
            //dataTable.Rows.Add(i + 1, DateTime.Now, (i + 1) + (i + 1) * 0.1 + (i + 1) * 0.01, "Name " + (i + 1), "Country " + (i + 1));

            DataRow row = dataTable.NewRow();
            row["OrderID"] = i + 1;
            row["OrderDate"] = DateTime.Now;
            row["Freight"] = (i + 1) + (i + 1) * 0.1 + (i + 1) * 0.01;
            row["ShipName"] = "Name " + (i + 1);
            row["ShipCountry"] = "Country " + (i + 1);

            dataTable.Rows.Add(row);
        }

        return dataTable;
    }

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            var dataItem = (GridDataItem)e.Item;
            var dataCell = dataItem["gtcaction"];
            RadImageButton btn = dataItem.FindControl("ImageButton1") as RadImageButton;
            btn.Text = "new text";
        }
    }

Regards,
Vessy
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Button Grid ImageButton
Asked by
Edward
Top achievements
Rank 1
Answers by
Vessy
Telerik team
Share this question
or