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

Accessing item level details from template controls

0 Answers 43 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Neil
Top achievements
Rank 1
Neil asked on 08 Feb 2013, 05:40 PM
I recently had a need to build a dynamic "form" for a simple database driven search engine.  I tried several different approaches and was satisfied with none until I tried using the RadListBox (probably could have used RadListView as well).  After spending quite some time trying to figure out how to manipulate the individual controls inside the RadListItems, I stumbled upon the solution and thought I would share.  Hope this helps some one out there!
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SearchUtil.aspx.cs" Inherits="CustomCo.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="Account Team" Value="AccountTeam" />
                    </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>

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 CustomCo
{
    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]);
            }
        }
    }
}

No answers yet. Maybe you can help?

Tags
ListBox
Asked by
Neil
Top achievements
Rank 1
Share this question
or