how to access controls in NoRecordsTemplate

14 posts, 1 answers
  1. Sumanth
    Sumanth avatar
    27 posts
    Member since:
    Sep 2010

    Posted 22 Nov 2010 Link to this post

    Hi,

    I want to access controls which are placed in NoRecordsTemplate from code behind page. Could you please help me in this regard.

    Thanks in advance.

    Thanks,
    Sumanth
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 22 Nov 2010 Link to this post

    Hello Sumanth,

    You can try the following code to access the control inside NoRecordsItemTemplate.

    ASPX:
    <NoRecordsTemplate>
        <asp:Label ID="Label1" runat="server" Text="No records"></asp:Label>
    </NoRecordsTemplate>

    C#:
    protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            GridNoRecordsItem norecordItem = (GridNoRecordsItem)RadGrid1.MasterTableView.GetItems(GridItemType.NoRecordsItem)[0];
            Label lbl = (Label)norecordItem.FindControl("Label1");
        }

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Sumanth
    Sumanth avatar
    27 posts
    Member since:
    Sep 2010

    Posted 25 Nov 2010 Link to this post

    Hi Princy.

    This is working fine for me. Thanks alot.

    Thanks & Regards,
    Sumanth
  5. Brad H
    Brad H avatar
    31 posts
    Member since:
    Aug 2009

    Posted 22 Jan 2011 Link to this post

    Does this have to be on Pre_Render?  I get IndexOutOfRangeException when I try to use this during the NeedDataSource event.
  6. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 24 Jan 2011 Link to this post

    Hello Sumanth,

    You cannot use Princy's approach on NeedDataSource as the item is not yet created.
    Another option is to handle Item created instead of PreRender:
    protected void Grid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridNoRecordsItem)
            
            }
        }


    Regards,
    Nikolay
    the Telerik team
    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
  7. arubbert
    arubbert avatar
    5 posts
    Member since:
    Oct 2006

    Posted 25 Apr 2012 Link to this post

    Hi all,
    I need to enable a RadButton in <NoRecordsTemplate>

    <NoRecordsTemplate>
        <telerik:RadButton ID="BrokerDetailsContactNoRecordAdd" runat="server" Text="Add new Contact" Enabled="false"
            OnClientClicked="BrokerDetailsContactAddOnClick" AutoPostBack="false" style="margin: 10px 0 0 10px;">
                <Icon PrimaryIconCssClass="rbAdd" PrimaryIconLeft="4" PrimaryIconTop="4" />
        </telerik:RadButton>
    </NoRecordsTemplate>

    Over OnPreRender in the MasterTabelView

    protected void BrokerDetailsContactGridPreRender(object sender, EventArgs e)
        {
            GridNoRecordsItem gridContactAdd = (GridNoRecordsItem)BrokerDetailsContactGrid.MasterTableView.GetItems(GridItemType.NoRecordsItem)[0];
             RadButton buttonContactAdd = (RadButton)gridContactAdd.FindControl("BrokerDetailsContactNoRecordAdd");
             buttonContactAdd.Enabled = true;
        }

    ... but the debugger returned the following error message

    IndexOutOfRangeException was unhandled by user code / Index was outside the bounds of the array

    The RadGrid is inside a RadMultiPage/RadPageView

    Any ideas ?
    Best regards
    Andreas
    Version Q1-2012






  8. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 25 Apr 2012 Link to this post

    Hi,

    Try enabling the RadButton in ItemCreated event. Please take a look into the following code snippet.

    C#:
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridNoRecordsItem)
        {
            GridNoRecordsItem item = (GridNoRecordsItem)e.Item;
            RadButton Button1 = (RadButton)item.FindControl("BrokerDetailsContactNoRecordAdd");
            Button1.Enabled = true;
        }
    }

    Thanks,
    Princy.
  9. arubbert
    arubbert avatar
    5 posts
    Member since:
    Oct 2006

    Posted 25 Apr 2012 Link to this post

    Hi Princy,

    thx for the fast reply.

    protected void BrokerDetailsContactGridOnItemCreated(object sender, GridItemEventArgs e)
            {
                if (e.Item is GridNoRecordsItem)
                {
                    GridNoRecordsItem gridContactAdd = (GridNoRecordsItem)e.Item;
                    RadButton buttonContactAdd = (RadButton)gridContactAdd.FindControl("BrokerDetailsContactNoRecordAdd");
                    buttonContactAdd.Enabled = true;
                    BrokerDetailsEditButton.Enabled = false;
                }
            }

    It looks like that the if clause are not working. BrokerDetailsEditButton.Enabled=false works outside the if clause.

    regards
    Andreas


  10. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 25 Apr 2012 Link to this post

    Hi,

    From your code I could find only a RadButton in the NoRecordTemplate. Please make sure that the 'BrokerDetailsEditButton' is also inside the NoRecordTemplate.

    Please provide the markup for the 'BrokerDetailsEditButton' for further assistance.

    Thanks,
    Princy.
  11. arubbert
    arubbert avatar
    5 posts
    Member since:
    Oct 2006

    Posted 25 Apr 2012 Link to this post

    Hi,

    I only placed the 'BrokerDetailsEditButton' into the if clause for testing. The BrokerDetailsContactNoRecordAdd button doesn´t enable.

    <telerik:RadMultiPage ID="BrokerDetailsMultiPage" runat="server" SelectedIndex="0" style="margin: 0 10px 10px 10px;">
                    <telerik:RadPageView ID="BrokerDetailsContactPageView" runat="server">
                        <telerik:RadGrid ID="BrokerDetailsContactGrid" runat="server" DataSourceID="BrokerContactEDS" GridLines="None" Height="238px" Width="100%"
                            AllowPaging="True" PageSize="25" AllowSorting="True" AutoGenerateColumns="False" ShowStatusBar="True"
                            OnItemCreated="BrokerDetailsContactGridOnItemCreated"
                            EnableHeaderContextMenu="True" EnableHeaderContextFilterMenu="True" AllowFilteringByColumn="True">
                            <PagerStyle AlwaysVisible="true" />
                            <ClientSettings EnableRowHoverStyle="true" EnablePostBackOnRowClick="false">
                                <ClientEvents OnRowContextMenu="BrokerDetailsContactGridRowContextMenu" OnRowClick="BrokerDetailsContactGridOnRowClick"
                                    OnScroll="handleScrolling" />
                                <Scrolling AllowScroll="true" UseStaticHeaders="true" SaveScrollPosition="true" />
                                <Selecting AllowRowSelect="true" />
                            </ClientSettings>
                            <GroupingSettings CaseSensitive="false" />
                            <MasterTableView runat="server" DataSourceID="BrokerContactEDS" Width="100%"
                                AllowFilteringByColumn="true" IsFilterItemExpanded="false" ShowHeadersWhenNoRecords="false"
                                DataKeyNames="BmsBrokerID, BmsBrokerGroupID, BmsBrokerContactID"
                                ClientDataKeyNames="BmsBrokerID, BmsBrokerGroupID, BmsBrokerContactID">
                                <NoRecordsTemplate>
                                    <telerik:RadButton ID="BrokerDetailsContactNoRecordAdd" runat="server" Text="Add new Contact" Enabled="true"
                                        OnClientClicked="BrokerDetailsContactAddOnClick" AutoPostBack="false" style="margin: 10px 0 0 10px;">
                                        <Icon PrimaryIconCssClass="rbAdd" PrimaryIconLeft="4" PrimaryIconTop="4" />
                                    </telerik:RadButton>
                                </NoRecordsTemplate>
                                <PagerTemplate>
                                    <div style="line-height: 24px; padding-right: 10px; float: right;">
                                        Total records: <%# DataBinder.Eval(Container, "Paging.DataSourceCount") %>
                                    </div>
                                </PagerTemplate>
                                <Columns>

    This is the code where my <NoRecordsTemplate> is placed.

    Thx
    Andreas

  12. arubbert
    arubbert avatar
    5 posts
    Member since:
    Oct 2006

    Posted 25 Apr 2012 Link to this post

    Solution or Bug ?

    ShowHeadersWhenNoRecords="false"

    MUST be set to "true" and than the code works :-(

    regards
    Andreas

  13. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 26 Apr 2012 Link to this post

    Hello Andreas,

    If ShowHeadersWhenNoRecords Property is set to true (the default) GridNoRecordsItem is used to display no records template. This item is the only one displayed in the GridTableView in this case. Since you set ShowHeadersWhenNoRecords to false, GridNoRecordsItem was not accessible and showed error.

    Thanks,
    Princy.
  14. T. Stanley
    T. Stanley avatar
    22 posts
    Member since:
    Sep 2007

    Posted 01 Jun 2012 Link to this post

    Hi.  A followup question regarding controls in a NoRecordsTemplate.  I have a RadButton inside a NoRecordsTemplate, and in its OnClick code-behind, I need it to access other controls (RadTextBoxes) also in the NoRecordsTemplate.  However, they are not accessible, and I don't know how to find them in code.  Can you help?

    Here's my markup of the NoRecordsTemplate in the RadGrid:
    <NoRecordsTemplate>
        <div style="padding: 20px 20px 20px 20px; height: 300px;">
            <div style="float: left;">
                <h4>
                    No patient was found with the MRN
                    <asp:Label ID="lblMRN" runat="server" Text="'mrn'" Font-Bold="True"></asp:Label></h4>
                Enter new patient information below:
                <br />
                <br />
                <table>
                    <tr>
                        <td class="tablelabel">
                            Last Name:
                        </td>
                        <td class="tabledata">
                            <telerik:RadTextBox ID="rtbLastName" runat="server" SelectionOnFocus="SelectAll" TabIndex="1">
                            </telerik:RadTextBox>
                        </td>
                        <td class="tablevalidator">
                            <asp:RequiredFieldValidator ID="rfvLastName" runat="server" Display="Dynamic" ControlToValidate="rtbLastName" ErrorMessage="Last Name is REQUIRED" CssClass="validation"></asp:RequiredFieldValidator>
                            <asp:RegularExpressionValidator Display="Dynamic" ID="revLastName" runat="server" ErrorMessage="Alphabetic characters only" ControlToValidate="rtbLastName" ValidationExpression="[a-zA-Z']+" CssClass="validation" /> <%--[a-zA-Z\-'\s]+--%>
                        </td>
                    </tr>
                    <tr>
                        <td class="tablelabel">
                            First Name:
                        </td>
                        <td class="tabledata">
                            <telerik:RadTextBox ID="rtbFirstName" runat="server" TabIndex="2">
                            </telerik:RadTextBox>
                        </td>
                        <td class="tablevalidator">
                            <asp:RequiredFieldValidator ID="rfvFirstName" runat="server" Display="Dynamic" ControlToValidate="rtbFirstName" ErrorMessage="First Name is REQUIRED" CssClass="validation"></asp:RequiredFieldValidator>
                            <asp:RegularExpressionValidator Display="Dynamic" ID="revFirstName" runat="server" ErrorMessage="Alphabetic characters only" ControlToValidate="rtbFirstName" ValidationExpression="[a-zA-Z']+" CssClass="validation" />
                        </td>
                    </tr>
                    <tr>
                        <td class="tablelabel">
                            MRN:
                        </td>
                        <td class="tabledata">
                            <telerik:RadTextBox ID="rtbMRN" runat="server" Enabled="false">
                                <DisabledStyle Font-Bold="True" ForeColor="Black" />
                            </telerik:RadTextBox>
                        </td>
                        <td class="tablevalidator">
                        </td>
                    </tr>
                    <tr>
                        <td class="tablelabel">
                            Date of Birth:
                        </td>
                        <td class="tabledata">
                            <telerik:RadDatePicker ID="rdpDOB" runat="server" MinDate="1912-01-01" TabIndex="3">
                                <Calendar UseColumnHeadersAsSelectors="False" UseRowHeadersAsSelectors="False" ViewSelectorText="x">
                                </Calendar>
                                <DateInput DateFormat="M/d/yyyy" DisplayDateFormat="M/d/yyyy" LabelWidth="" TabIndex="3">
                                </DateInput>
                                <DatePopupButton HoverImageUrl="" ImageUrl="" TabIndex="3" />
                            </telerik:RadDatePicker>
                        </td>
                        <td class="tablevalidator">
                            <asp:RequiredFieldValidator ID="rfvDOB" runat="server" Display="Dynamic" ControlToValidate="rdpDOB" ErrorMessage="Date of Birth is REQUIRED" CssClass="validation"></asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td class="tablelabel">
                            Phone:
                        </td>
                        <td class="tabledata">
                            <telerik:RadMaskedTextBox ID="rmtPhone" runat="server" Mask="(###) ###-####" ShowButton="true" ButtonCssClass="textboxclear" TabIndex="4">
                                <ClientEvents OnButtonClick="ClearInput" />
                            </telerik:RadMaskedTextBox>
                        </td>
                        <td class="tablevalidator">
                            <%--<asp:RequiredFieldValidator Display="Dynamic" ID="rfvPhone" runat="server" ErrorMessage="Please, enter a phone number." ControlToValidate="rmtPhone" CssClass="validation" ></asp:RequiredFieldValidator>--%>
                            <asp:RegularExpressionValidator Display="Dynamic" ID="revPhone" runat="server" ErrorMessage="Format is (###) ###-####" ControlToValidate="rmtPhone" ValidationExpression="^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$"
                                CssClass="validation" />
                        </td>
                    </tr>
                </table>
                <br />
                <telerik:RadButton ID="rbtnAcceptNewPatient" runat="server" Text="Accept" OnClientClicking="StandardConfirm" OnClick="rbtnAcceptNewPatient_Click" TabIndex="5">
                    <Icon PrimaryIconCssClass="rbOk" PrimaryIconLeft="4" PrimaryIconTop="4" />
                </telerik:RadButton>
                    
                <telerik:RadButton ID="rbtnCancelNewPatient" runat="server" Text="Cancel" OnClick="rbtnCancelPatient_Click" CausesValidation="false" TabIndex="6">
                    <Icon PrimaryIconCssClass="rbCancel" PrimaryIconLeft="4" PrimaryIconTop="4" />
                </telerik:RadButton>
            </div>
        </div>
    </NoRecordsTemplate>

    and this in the code-behind:
    protected void rbtnAcceptNewPatient_Click(object sender, EventArgs e)
    {
        Patient pt = new Patient();
        pt.AddPatient(rtbMRN.Text, TaljacUtilities.NameCapitalization(rtbLastName.Text), TaljacUtilities.NameCapitalization(rtbFirstName.Text), rdpDOB.Value);
    }

    Thanks!!

    Tom Stanley
  15. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 04 Jun 2012 Link to this post

    Hi,

    Try the following code snippet to access the other controls in the NoRecordsTemplate on a RadButton Click.

    C#:
    protected void rbtnAcceptNewPatient_Click(object sender, EventArgs e)
    {
        RadButton Button = (RadButton)sender;
        GridNoRecordsItem item = (GridNoRecordsItem)Button.NamingContainer;
        RadTextBox txtbox = (RadTextBox)item.FindControl("rtbLastName");   // accessing the RadTextBox
    }

    Note: Please set the CausesValidation property of RadButton to "false". Check this help documentation for details.

    Thanks,
    Princy.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017