This is a migrated thread and some comments may be shown as answers.

How to get TemplateColumn value in ItemDataBound event?

1 Answer 549 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Indranath
Top achievements
Rank 1
Indranath asked on 04 Jun 2011, 02:32 PM
I am writing Tooltip for RadGrid control columns, my following sample grid has Bound and Template columns. Following code working fine with Bound columns but not for Template column.

 <telerik:RadGrid ID="radCntDets" runat="server" GridLines="None" Width="938px"
                AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
                OnNeedDataSource="radCntDets_OnNeedDataSource"
                AllowAutomaticDeletes="True" AllowAutomaticInserts="True" AllowAutomaticUpdates="True"
                HorizontalAlign="NotSet"
                Font-Size="Small"
                OnItemDataBound="radCntDets_ItemDataBound"
                OnItemUpdated="radCntDets_ItemUpdated"
                 OnUpdateCommand="radCntDets_UpdateCommand"
                 OnInsertCommand="radCntDets_InsertCommand"
                 OnDeleteCommand="radCntDets_DeleteCommand">
                 <ClientSettings EnableRowHoverStyle="True">
                    <ClientEvents OnColumnClick="rowColumnClicked" OnRowClick="rowClick" OnCommand="oncommand" />
                    <Scrolling AllowScroll="true" UseStaticHeaders="True" />
                </ClientSettings>
                <MasterTableView  GridLines="None" CommandItemDisplay="Top" DataKeyNames="ControlID" EditMode="InPlace">
                    <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
                    <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column"></RowIndicatorColumn>
                    <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column"></ExpandCollapseColumn>
                    <Columns>
                        <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" ItemStyle-Width="100px" HeaderStyle-Width="100px">
                            <HeaderStyle Width="100px"></HeaderStyle>
                            <ItemStyle Width="100px"></ItemStyle>
                        </telerik:GridEditCommandColumn>
                        <telerik:GridButtonColumn ConfirmText="Delete this control?" ButtonType="ImageButton"
                            CommandName="Delete" Text="Delete" UniqueName="DeleteColumn1">
                            <HeaderStyle Width="20px" />
                            <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" />
                        </telerik:GridButtonColumn>
                        <telerik:GridTemplateColumn HeaderText="Email Address" UniqueName="EmailAddress" ItemStyle-Width="80px" HeaderStyle-Width="80px">
                            <ItemTemplate>
                                <%#DataBinder.Eval(Container.DataItem, "EmailAddress")%>
                            </ItemTemplate>
                            <EditItemTemplate>
                                 <%--<asp:TextBox ID="txtEAddress" runat="server" Text='<%# Bind("EmailAddress") %>'></asp:TextBox>--%>
                                <input id="txtEmails" runat="server"  type="text" class="bodyText" width="120px" onchange="return ValidateEmail(this);" />
                                <asp:RegularExpressionValidator Display="Dynamic" ID="RegularExpressionValidator1" ValidationExpression="^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
                                            runat="server" ControlToValidate="txtEmails" ErrorMessage="Valid E-mail address is required.">*</asp:RegularExpressionValidator>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridBoundColumn UniqueName="Description" HeaderText="Description" DataField="Description"
                            HeaderStyle-Width="220px" ItemStyle-Width="220px">
                            <HeaderStyle Width="220px"></HeaderStyle>
                            <ItemStyle Width="220px"></ItemStyle>
                        </telerik:GridBoundColumn>
                      
                    </Columns>
         </MasterTableView>
         <FilterMenu EnableImageSprites="False"></FilterMenu>
         <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default"></HeaderContextMenu>
        </telerik:RadGrid>

 protected void radCntDets_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem gridItem = e.Item as GridDataItem;
                foreach (GridColumn column in radCntDets.MasterTableView.RenderColumns)
                {
                    if (column is GridBoundColumn)
                        gridItem[column.UniqueName].ToolTip = gridItem[column.UniqueName].Text;

                    if (column is GridTemplateColumn)
                    {
                        // How to get ItemTemplate (EmailAddress) value
                    }
                }
            }
        }

Thanks in advance for your help.

- Indra

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 06 Jun 2011, 06:10 AM
Hello Indranath,

Access the value in ItemTemplate using the DataRowView as shown below.

C#:
if (column is GridTemplateColumn)
{
   string s = ((DataRowView)e.Item.DataItem)["EmailAddress"].ToString();
}


Thanks,
Princy.
Tags
Grid
Asked by
Indranath
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or