Server-Side Find control RadListView on InsertItemTemplate

4 posts, 0 answers
  1. Valerio
    Valerio avatar
    4 posts
    Member since:
    Mar 2018

    Posted 20 Dec 2018 Link to this post

    Hi,

     

    I am trying to access a control in InsertItemTemplate.

     

    On EditTemplate is fine, I use:

    var Ctlcst_costru_civmec_note = (RadTextBox)CtlCstCostruzione.EditItems[0].FindControl("Ctlcst_costru_civmec_note");

     

    But I am not able to find the same control on insert one. How can I do?

     

    Thx,

    Valerio

  2. Marin Bratanov
    Admin
    Marin Bratanov avatar
    5685 posts

    Posted 23 Dec 2018 Link to this post

    Hello Valerio,

    The best place to do that is the ItemDataBound event as shown in the following article: https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-controls-in-editinsert-mode

    That said, if you need to run some code outside of the grid, you can use the GetInsertItem method of the table view. For example:

    protected void btn1_Click(object sender, EventArgs e)
    {
        if (rg1.MasterTableView.IsItemInserted)
        {
            GridEditableItem item = rg1.MasterTableView.GetInsertItem();
            if (item != null)
            {
                RadTextBox rtb = item.FindControl("rtbInTemplate") as RadTextBox;
                if (rtb != null)
                {
                    rtb.Text = DateTime.Now.ToString();
                }
            }
        }
    }

    with markup like this (the control ID is there, nothing else significant):

    <asp:Button Text="get the textbox" ID="btn1" OnClick="btn1_Click" runat="server" />
    <telerik:RadGrid runat="server" ID="rg1" OnNeedDataSource="rg1_NeedDataSource" AllowPaging="true">
        <MasterTableView CommandItemDisplay="Top">
            <Columns>
                <telerik:GridTemplateColumn>
                    <InsertItemTemplate>
                        <telerik:RadTextBox runat="server" ID="rtbInTemplate"></telerik:RadTextBox>
                    </InsertItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    and a little bit of dummy data to get it running:

    protected void rg1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        (sender as RadGrid).DataSource = Enumerable.Range(1, 300).Select(o => new { ID = o, someName = "name " + o });
    }


    Regards,
    Marin Bratanov
    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.
  3. Valerio
    Valerio avatar
    4 posts
    Member since:
    Mar 2018

    Posted 23 Dec 2018 in reply to Marin Bratanov Link to this post

    Hi Marin,

     

    it works on RadGrid, but how do I manage for a RadListView?

    I am not able to figure it out.

     

    Thx for help,

    Valerio

  4. Marin Bratanov
    Admin
    Marin Bratanov avatar
    5685 posts

    Posted 24 Dec 2018 Link to this post

    My bad, Valerio, for some reason I thought this is about a grid.

    Generally, for a listview the same approach about using its events applies: https://docs.telerik.com/devtools/aspnet-ajax/controls/listview/templates/accessing-controls#accessing-controls-in-the-edititemtemplateinsertitemtemplate

    When using an external button, instead of a method, there is the .InsertItem property:

    protected void btn1_Click(object sender, EventArgs e)
    {
        if(RadListView1.InsertItem != null)
        {
            TextBox tb = RadListView1.InsertItem.FindControl("TextBox11") as TextBox;
            if(tb != null)
            {
                tb.Text = DateTime.Now.ToString();
            }
        }
    }

    where this can run on the following simplified version of our online demo for automatic CRUD operations:

    <asp:Button Text="get insert item textbox" ID="btn1" OnClick="btn1_Click" runat="server" />
    <telerik:RadListView ID="RadListView1" RenderMode="Lightweight" DataSourceID="SqlDataSource1" runat="server"
        ItemPlaceholderID="ProductItemContainer" DataKeyNames="ProductID" AllowPaging="True">
        <ItemTemplate>
            <fieldset class="itemFieldset">
                <%# Eval("ProductName") %>
            </fieldset>
        </ItemTemplate>
        <EditItemTemplate>
            <fieldset class="itemFieldset">
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ProductName") %>'></asp:TextBox>
            </fieldset>
        </EditItemTemplate>
        <InsertItemTemplate>
            <fieldset class="itemFieldset">
                <asp:TextBox ID="TextBox11" runat="server" Text='<%# Bind("ProductName") %>'></asp:TextBox>
            </fieldset>
        </InsertItemTemplate>
        <LayoutTemplate>
            <fieldset />
            <legend>RadListView bound to SqlDataSource</legend>
            <table class="layoutTable">
                <tr>
                    <td class="button">
                        <telerik:RadButton RenderMode="Lightweight" ID="Button1" runat="server" CommandName="InitInsert" Visible="<%# !Container.IsItemInserted %>"
                            Text="Add new product">
                        </telerik:RadButton>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Panel ID="ProductItemContainer" runat="server">
                        </asp:Panel>
                    </td>
                </tr>
                <tr>
                    <td>
                        <telerik:RadDataPager RenderMode="Lightweight" ID="RadDataPager1" runat="server" PagedControlID="RadListView1"
                            PageSize="3">
                            <Fields>
                                <telerik:RadDataPagerButtonField FieldType="FirstPrev"></telerik:RadDataPagerButtonField>
                                <telerik:RadDataPagerButtonField FieldType="Numeric"></telerik:RadDataPagerButtonField>
                                <telerik:RadDataPagerButtonField FieldType="NextLast"></telerik:RadDataPagerButtonField>
                            </Fields>
                        </telerik:RadDataPager>
                    </td>
                </tr>
            </table>
            </fieldset>
        </LayoutTemplate>
    </telerik:RadListView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
        DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @ProductID" InsertCommand="INSERT INTO [Products] ([ProductName], [SupplierID], [CategoryID], [QuantityPerUnit], [UnitPrice], [UnitsInStock], [UnitsOnOrder], [ReorderLevel], [Discontinued]) VALUES (@ProductName, @SupplierID, @CategoryID, @QuantityPerUnit, @UnitPrice, @UnitsInStock, @UnitsOnOrder, @ReorderLevel, @Discontinued)"
        SelectCommand="SELECT * FROM [Products]" UpdateCommand="UPDATE [Products] SET [ProductName] = @ProductName, [SupplierID] = @SupplierID, [CategoryID] = @CategoryID, [QuantityPerUnit] = @QuantityPerUnit, [UnitPrice] = @UnitPrice, [UnitsInStock] = @UnitsInStock, [UnitsOnOrder] = @UnitsOnOrder, [ReorderLevel] = @ReorderLevel, [Discontinued] = @Discontinued WHERE [ProductID] = @ProductID">
        <DeleteParameters>
            <asp:Parameter Name="ProductID" Type="Int32"></asp:Parameter>
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="ProductName" Type="String"></asp:Parameter>
            <asp:Parameter Name="SupplierID" Type="Int32"></asp:Parameter>
            <asp:Parameter Name="CategoryID" Type="Int32"></asp:Parameter>
            <asp:Parameter Name="QuantityPerUnit" Type="String"></asp:Parameter>
            <asp:Parameter Name="UnitPrice" Type="Decimal"></asp:Parameter>
            <asp:Parameter Name="UnitsInStock" Type="Int16"></asp:Parameter>
            <asp:Parameter Name="UnitsOnOrder" Type="Int16"></asp:Parameter>
            <asp:Parameter Name="ReorderLevel" Type="Int16"></asp:Parameter>
            <asp:Parameter Name="Discontinued" Type="Boolean"></asp:Parameter>
            <asp:Parameter Name="ProductID" Type="Int32"></asp:Parameter>
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="ProductName" Type="String"></asp:Parameter>
            <asp:Parameter Name="SupplierID" Type="Int32"></asp:Parameter>
            <asp:Parameter Name="CategoryID" Type="Int32"></asp:Parameter>
            <asp:Parameter Name="QuantityPerUnit" Type="String"></asp:Parameter>
            <asp:Parameter Name="UnitPrice" Type="Decimal"></asp:Parameter>
            <asp:Parameter Name="UnitsInStock" Type="Int16"></asp:Parameter>
            <asp:Parameter Name="UnitsOnOrder" Type="Int16"></asp:Parameter>
            <asp:Parameter Name="ReorderLevel" Type="Int16"></asp:Parameter>
            <asp:Parameter Name="Discontinued" Type="Boolean"></asp:Parameter>
        </InsertParameters>
    </asp:SqlDataSource>

    Regards,
    Marin

Back to Top