Accessing Controls inside RadListItems

Thread is closed for posting
1 posts, 0 answers
  1. ED08E970-2947-4D18-B49E-A06990DA34EC
    ED08E970-2947-4D18-B49E-A06990DA34EC avatar
    16 posts
    Member since:
    Mar 2007

    Posted 08 Feb 2013 Link to this post

    Requirements

    RadControls version

     2012.3.1016.40

    .NET version

     4.0

    Visual Studio version 2008
    programming language

     c#

    browser support

    all browsers supported by RadControls


    PROJECT DESCRIPTION                                    

    A method was needed to control items within the templated RadListItem.  Hope this helps!
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AdvancedSearch.aspx.cs" Inherits="Catalysttg.SearchUtil" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <telerik:RadListBox ID="rlbSearchParams" runat="server" Width="80%" ClientIDMode="Predictable">
            <ItemTemplate>
                <span>
                    <asp:Image ID="imgBlank0" runat="server" ImageUrl="~/Images/Templates/blank0.png" Width="75px" Height="1px" Visible="false" />
                    <telerik:RadComboBox ID="ddlAndOr" runat="server" Width="75px">
                        <Items>
                            <telerik:RadComboBoxItem Text="AND" Value="0" />
                            <telerik:RadComboBoxItem Text="OR" Value="1" />
                        </Items>
                    </telerik:RadComboBox>
                    <telerik:RadComboBox ID="ddlParamName" runat="server" Width="200px">
                        <Items>
                            <telerik:RadComboBoxItem Text="Author Name" Value="AuthorName" />
                            <telerik:RadComboBoxItem Text="Title" Value="Title" />
                            <telerik:RadComboBoxItem Text="ISBN" Value="ISBN" />
                        </Items>
                    </telerik:RadComboBox>
                    <telerik:RadTextBox ID="txtParamValue" runat="server" Width="200px" />
                    <asp:Button ID="btnAdd" runat="server" Text="+" CommandName="Add" OnClick="btnAdd_Click" />
                    <asp:Button ID="btnRemove" runat="server" Text="-" CommandName="Remove" OnClick="btnRemove_Click" />
                </span>
            </ItemTemplate>
        </telerik:RadListBox>
        </form>
    </body>
    </html>
    And the code file
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Web.UI;
     
    namespace Catalysttg
    {
        public partial class AdvancedSearch : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (rlbSearchParams.Items.Count < 1)
                    CreateForm();
            }
            private void CreateForm()
            {
                RadListBoxItem firstItem = new RadListBoxItem();
                rlbSearchParams.Items.Add(firstItem);
                RadComboBox myAndOR = rlbSearchParams.Items[0].FindControl("ddlAndOr") as RadComboBox;
                myAndOR.Visible = false;
                Image Spacer = rlbSearchParams.Items[0].FindControl("imgBlank0") as Image;
                Spacer.Visible = true;
                Button myRemoveButton = rlbSearchParams.Items[0].FindControl("btnRemove") as Button;
                myRemoveButton.Visible = false;
                myRemoveButton.CommandArgument = "0";
            }
            protected void btnAdd_Click(object sender, EventArgs e)
            {
                RadListBoxItem secondItem = new RadListBoxItem();
                rlbSearchParams.Items.Add(secondItem);
                Button myRemoveButton = rlbSearchParams.Items[0].FindControl("btnRemove") as Button;
                myRemoveButton.CommandArgument = (rlbSearchParams.Items.Count - 1).ToString();
                Button myPreviousAddButton = rlbSearchParams.Items[(rlbSearchParams.Items.Count - 2)].FindControl("btnAdd") as Button;
                myPreviousAddButton.Visible = false;
            }
            protected void btnRemove_Click(object sender, EventArgs e)
            {
                Button mySender = (Button)sender;
                RadListBoxItem myItem = (RadListBoxItem)mySender.Parent;
                if (myItem != null)
                {
                    switch(myItem.Index)
                    {
                        case 0:
                            CreateForm();
                            break;
                        case 1:
                            Button myPreviousAddButton = rlbSearchParams.Items[0].FindControl("btnAdd") as Button;
                            myPreviousAddButton.Visible = true;
                            break;
                        default:
                            if (rlbSearchParams.Items.Count - 1 == myItem.Index)
                            {
                                Button defaultPreviousAddButton = rlbSearchParams.Items[myItem.Index - 1].FindControl("btnAdd") as Button;
                                defaultPreviousAddButton.Visible = true;
                            }
                            if (rlbSearchParams.Items.Count > 1)
                            {
                                Button defaultPreviousRemoveButton = rlbSearchParams.Items[myItem.Index - 1].FindControl("btnRemove") as Button;
                                defaultPreviousRemoveButton.Visible = true;
                            }
                            break;
                    }
                    rlbSearchParams.Items.Remove(rlbSearchParams.Items[myItem.Index]);
                }
            }
        }
    }


Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.