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

FindControl in a GridTemplateColumn during PreRender

6 Answers 742 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andy
Top achievements
Rank 2
Andy asked on 08 Aug 2011, 10:14 PM
How do I get a reference to a button in a template column of my grid during PreRender?

Here is a grid (ShotDetail_RadGrid) that has tabs on each row of data. On the first of these tabs is another grid (Attachments_RadGrid) with the ImageButton I'm trying to get the reference to:
<telerik:RadGrid ID="ShotDetail_RadGrid" runat="server" DataSourceID="ShotDetail_SqlDataSource" Skin="Black"
    GridLines="None" ShowStatusBar="True" Width="1100px" CellSpacing="0" AutoGenerateColumns="False">
    <HierarchySettings ExpandTooltip="Show related merch" CollapseTooltip="Collapse merch"></HierarchySettings>
    <ExportSettings IgnorePaging="True">
        <Pdf PageBottomMargin="0.5in" PageHeight="8.5in" PageLeftMargin="0.35in" PageRightMargin="0.35in" PageTopMargin="0.5in"
            PageWidth="11in" />
    </ExportSettings>
    <ClientSettings>
        <Scrolling AllowScroll="True" UseStaticHeaders="True" />
    </ClientSettings>
    <MasterTableView DataKeyNames="ShotID" DataSourceID="ShotDetail_SqlDataSource" AllowCustomPaging="True">
        <NestedViewTemplate>
            <asp:Panel runat="server" ID="InnerContainer" CssClass="viewWrap" Visible="false">
                <telerik:RadTabStrip runat="server" ID="TabStip1" MultiPageID="Multipage1" SelectedIndex="0">
                    <Tabs>
                        <telerik:RadTab runat="server" Text="Merchandise" PageViewID="PageView1" Selected="True">
                        </telerik:RadTab>
                        <telerik:RadTab runat="server" Text="Shot Support" PageViewID="PageView2">
                        </telerik:RadTab>
                        <telerik:RadTab runat="server" Text="Completed Images">
                        </telerik:RadTab>
                    </Tabs>
                </telerik:RadTabStrip>
                <telerik:RadMultiPage runat="server" ID="Multipage1" SelectedIndex="0" RenderSelectedPageOnly="false">
                    <telerik:RadPageView runat="server" ID="PageView1">
                        <asp:Label ID="ShotID_Label" Text='<%# Eval("ShotID") %>' Visible="false" runat="server" />
                        <asp:SqlDataSource ID="Merch_SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:Alderman_ConnectionString %>"
                            SelectCommand="sp_web_Merch_Grid_sel" SelectCommandType="StoredProcedure">
                            <SelectParameters>
                                <asp:ControlParameter ControlID="ShotID_Label" PropertyName="Text" Type="Int32" Name="ShotID" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                        <telerik:RadGrid ID="Merch_RadGrid" runat="server" AutoGenerateColumns="False" DataSourceID="Merch_SqlDataSource"
                            GridLines="None" CellSpacing="0">
                            <ClientSettings AllowColumnsReorder="True" ReorderColumnsOnClient="True">
                            </ClientSettings>
                            <MasterTableView DataSourceID="Merch_SqlDataSource">
                                <CommandItemSettings ExportToPdfText="Export to Pdf" />
                                <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                                </RowIndicatorColumn>
                                <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                                </ExpandCollapseColumn>
                                <Columns>
                                    <telerik:GridBoundColumn DataField="Barcode" DataType="System.Int64" Display="False" HeaderText="Barcode"
                                        UniqueName="Barcode">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn DataField="Source" HeaderText="Vendor" UniqueName="Source" FilterControlAltText="Filter Source column">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn DataField="Description" HeaderText="Description" UniqueName="Description">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn DataField="SKU" HeaderText="SKU" UniqueName="SKU">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn DataField="SKU2" HeaderText="SKU2" UniqueName="SKU2">
                                    </telerik:GridBoundColumn>
                                </Columns>
                                <EditFormSettings>
                                    <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                                    </EditColumn>
                                </EditFormSettings>
                            </MasterTableView>
                            <FilterMenu EnableImageSprites="False">
                            </FilterMenu>
                            <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default" EnableImageSprites="True">
                            </HeaderContextMenu>
                        </telerik:RadGrid>
                    </telerik:RadPageView>
                    <telerik:RadPageView runat="server" Width="100%" ID="PageView2">
                        <asp:SqlDataSource ID="Attachments_SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:Alderman_ConnectionString %>"
                            SelectCommand="SELECT [AttachmentTitle], [LinkedFromID] FROM [tAttachments] WHERE (([LinkedFromID] = @ShotID) AND ([LinkedFromTable] = @LinkedFromTable) AND (AttachmentIsInvalid = 0))">
                            <SelectParameters>
                                <asp:ControlParameter ControlID="ShotID_Label" PropertyName="Text" Type="Int32" Name="ShotID" />
                                <asp:Parameter DefaultValue="tShots" Name="LinkedFromTable" Type="String" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                        <telerik:RadGrid ID="Attachments_RadGrid" runat="server" DataSourceID="Attachments_SqlDataSource" GridLines="None"
                            CellSpacing="0">
                            <MasterTableView DataSourceID="Attachments_SqlDataSource" AutoGenerateColumns="False">
                                <CommandItemSettings ExportToPdfText="Export to PDF" />
                                <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                                </RowIndicatorColumn>
                                <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                                </ExpandCollapseColumn>
                                <Columns>
                                    <telerik:GridTemplateColumn UniqueName="ColumnDownload">
                                        <ItemTemplate>
                                            <asp:ImageButton ID="DownloadForShot_ImageButton" runat="server" ImageUrl="~/Images/view.gif" OnClick="DownloadForShot_ImageButton_Click" />
                                            <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
                                        </ItemTemplate>
                                    </telerik:GridTemplateColumn>
                                    <telerik:GridBoundColumn DataField="AttachmentTitle" HeaderText="Documents" UniqueName="ColumnAttachment">
                                    </telerik:GridBoundColumn>
                                </Columns>
                                <EditFormSettings>
                                    <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                                    </EditColumn>
                                </EditFormSettings>
                            </MasterTableView>
                            <FilterMenu EnableImageSprites="False">
                            </FilterMenu>
                            <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
                            </HeaderContextMenu>
                        </telerik:RadGrid>
                    </telerik:RadPageView>
                </telerik:RadMultiPage>
            </asp:Panel>
        </NestedViewTemplate>
        <CommandItemSettings ExportToPdfText="Export to Pdf"></CommandItemSettings>
        <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
        </RowIndicatorColumn>
        <ExpandCollapseColumn Visible="True">
        </ExpandCollapseColumn>
        <Columns>
            <telerik:GridBoundColumn DataField="JobShot" HeaderText="Shot" UniqueName="JobShot">
                <HeaderStyle Width="100px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Description" HeaderText="Description" UniqueName="Description">
                <HeaderStyle Width="700px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="MerchCount" DataType="System.Int16" HeaderText="Merch Count" UniqueName="MerchCount"
                AllowFiltering="False">
                <HeaderStyle Width="50px" />
                <ItemStyle Wrap="True" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="JobNumber" Display="False" HeaderText="JobNumber" UniqueName="JobNumber"
                FilterControlAltText="Filter JobNumber column">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ShotID" DataType="System.Int32" HeaderText="ShotID" UniqueName="ShotID"
                Display="False">
            </telerik:GridBoundColumn>
        </Columns>
        <EditFormSettings>
            <EditColumn FilterControlAltText="Filter EditCommandColumn column">
            </EditColumn>
        </EditFormSettings>
        <PagerStyle AlwaysVisible="True" />
    </MasterTableView>
    <PagerStyle AlwaysVisible="True" />
    <FilterMenu EnableImageSprites="False">
    </FilterMenu>
    <HeaderContextMenu EnableImageSprites="True" CssClass="GridContextMenu GridContextMenu_Default">
    </HeaderContextMenu>
</telerik:RadGrid>

Here is my PreRender code. The viewImageButton is never set.
protected void ShotDetail_RadGrid_PreRender(object sender, EventArgs e)
{
    if (!Page.IsPostBack) {
        //*** Associate each Attachments_RadGrid with its own postback script to open the file.
        for (int i = 0; i <= ShotDetail_RadGrid.MasterTableView.Items.Count - 1; i++) {
            Panel aspPanel = ShotDetail_RadGrid.MasterTableView.Items(i).ChildItem.FindControl("InnerContainer");
            RadGrid attachmentsGrid = aspPanel.FindControl("Attachments_RadGrid");
 
            RadScriptManager ScriptManager1 = default(RadScriptManager);
            ScriptManager1 = (RadScriptManager)this.Parent.FindControl("RadScriptManager1");
            ScriptManager1.RegisterPostBackControl(attachmentsGrid);
 
            ImageButton viewImageButton = attachmentsGrid.MasterTableView.FindControl("DownloadForShot_ImageButton");
            viewImageButton.Attributes.Add("onclick", " ExportButtonClick ();");
        }
    }
}

Thanks!

6 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 09 Aug 2011, 06:01 AM
Hello Andy,

I suppose your ultimate aim is to attach "onclick" event for ImageButton. Then one easy approach is to attach ItemCreated event to the Attachments_RadGrid and access the ImageButton as shown below.
C#:
protected void Attachments_RadGrid_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
  if (e.Item is GridDataItem)
{
   GridDataItem item = (GridDataItem)e.Item;
   ImageButton img = (ImageButton)item.FindControl("DownloadForShot_ImageButton");
   img.Attributes.Add("onclick", "ExportButtonClick ();");
}
}

Thanks,
Princy.
0
Andy
Top achievements
Rank 2
answered on 15 Aug 2011, 02:47 PM
Excellent idea! That did exactly what I needed!
0
John
Top achievements
Rank 1
answered on 24 Jun 2019, 08:17 AM

Hi Andy,

I have the following code below within the grid preRender event. does the GridTextBoxColumnEditor).TextBoxControl have a currency property or is there a way to set the editting TextBox as a currency or at least 2 decimal digit format.

Also the numeric text box in the grid is a template column so there isn't a preRender event for the numeric textbox available as you suggest.

Thanks

John

 

    //  ProductID is a  telerik:GridBoundColumn
            try
            {
                GridColumn ProductIDColumn = masterTable.GetColumnSafe("ProductID") as GridColumn;
                TextBox ProductIDEditor = (masterTable.GetBatchColumnEditor(ProductIDColumn) as GridTextBoxColumnEditor).TextBoxControl;
                ProductIDEditor.Width = Unit.Percentage(100);
            }
            catch { } // The user may have discarded the column in User Settings

0
Attila Antal
Telerik team
answered on 25 Jun 2019, 02:40 PM
Hi John,

The GridTextBoxColumnEditor is a Class that implements data editing of a GridBoundColumn with a single TextBox control. This TextBox control does not have a currency property. Its value needs to be formatted manually by the developer. Instead, you could try using the GridNumericColumnEditor of the GridNumericColumns for decimal values.

If using Template columns in RadGrid, you can add any control of choice and configure it accordingly. The following example will show a template column with a RadNumericTextBox where the Numeric textbox has properties to manipulate the decimal digits, or separators.

<telerik:GridTemplateColumn UniqueName="MyTemplateColumn" HeaderText="My Template Column">
    <ItemTemplate>
        <%# Eval("Freight") %>
    </ItemTemplate>
    <EditItemTemplate>
        <telerik:RadNumericTextBox ID="RadNumericTextBox1" runat="server" Value='<%# Bind("Freight") %>' OnPreRender="RadNumericTextBox1_PreRender">
            <NumberFormat DecimalDigits="2" DecimalSeparator="," />
        </telerik:RadNumericTextBox>
    </EditItemTemplate>
</telerik:GridTemplateColumn>

It can also be done on server side:

GridNumericColumnEditor numericColEditor = new GridNumericColumnEditor();
 
numericColEditor.NumericTextBox.NumberFormat.DecimalDigits = 2;
numericColEditor.NumericTextBox.NumberFormat.DecimalSeparator = ",";

Regardless of the event, you can access the controls of RadGrid anytime (if they are rendered). For instructions to access cells, values or controls inside the grid please check out the Accessing Cells and Rows article.

Kind regards,
Attila Antal
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
John
Top achievements
Rank 1
answered on 25 Jun 2019, 03:17 PM

Hi Attila,

Thanks for the Idea but it had no effect. If you look at the attachment you can see the format of the order date working correctly in batch edit mode but the unit price is not showing as £11.00 it is showing as 11. The column has to be a template column because I have a Custom Validator on it. Any other ideas? I would settle for the NumericTextbox editing field showing  11.00 without the currency symbol.

Thanks in advance.

John

0
Attila Antal
Telerik team
answered on 01 Jul 2019, 01:54 PM
Hi John,

Thanks for the additional clarification. I now have a better understanding on the issue. 

Perhaps the property you are looking for is the KeepTrailingZerosOnFocus. Having this set to True, the NumericTextBox will display the trailing zeroes. 

Below I will share an example for configuring the two different Columns with RadGrid in Batch Edit mode.

GridTemplateColumn:

<telerik:GridTemplateColumn DataField="Freight" HeaderText="Template Column" UniqueName="TemplateColumn1"
    DataType="System.Decimal">
    <ItemTemplate>
        <%# string.Format("{0:C}", Eval("Freight")) %>
    </ItemTemplate>
    <EditItemTemplate>
        <telerik:RadNumericTextBox ID="RadNumericTextBox1" runat="server" DbValue='<%# Bind("Freight") %>'>
            <NumberFormat KeepTrailingZerosOnFocus="true" />
        </telerik:RadNumericTextBox>
    </EditItemTemplate>
</telerik:GridTemplateColumn>


GridNumericColumn:

<telerik:GridBoundColumn DataField="Freight" DataType="System.Decimal" DataFormatString="{0:C}"
    FilterControlAltText="Filter Freight column" HeaderText="Bound Column"
    SortExpression="Freight" UniqueName="BoundColumn1">
</telerik:GridBoundColumn>

Accessing Controls of BatchEdit mode in the Server:

C# 

protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    GridTableView masterTable = ((RadGrid)sender).MasterTableView;
    GridNumericColumnEditor colEditor = masterTable.GetBatchColumnEditor("NumericColumn1") as GridNumericColumnEditor;
    RadNumericTextBox numericTextBox = colEditor.NumericTextBox;
    numericTextBox.NumberFormat.KeepTrailingZerosOnFocus = true;       
}

VB

Protected Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As EventArgs)
    Dim masterTable As GridTableView = (CType(sender, RadGrid)).MasterTableView
    Dim colEditor As GridNumericColumnEditor = TryCast(masterTable.GetBatchColumnEditor("NumericColumn1"), GridNumericColumnEditor)
    Dim numericTextBox As RadNumericTextBox = colEditor.NumericTextBox
    numericTextBox.NumberFormat.KeepTrailingZerosOnFocus = True
End Sub

RadNumericTextBox only supports numbers which can be decimal, negative or positive. In order to be able to treat this as numbers in case of calculations, sorting, and more, no special characters are displayed while the RadNumericTextbox is focused. 

If you would like to have the special characters at all times, you may consider using RadTextBoxes/BoundColumns for this. With that you will not benefit of the advantage the Number values. The TextBox will treat the values as string instead.

You can also try the NumericTextBox individually (outside of the Grid) to see it in action. It displays currency when the Type property is set accordingly, however, when clicked/focused it will bring up the digits instead.

<telerik:RadNumericTextBox ID="RadNumericTextBox1" runat="server" Type="Currency" Value="11.00">
    <NumberFormat KeepTrailingZerosOnFocus="true" />
</telerik:RadNumericTextBox>


Kind regards,
Attila Antal
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Andy
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Andy
Top achievements
Rank 2
John
Top achievements
Rank 1
Attila Antal
Telerik team
Share this question
or