This is a migrated thread and some comments may be shown as answers.
Filter Template with Combo Box - with Images
6 Answers 10 Views
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 12 Mar 2018, 05:17 PM

I am trying to implement a Filter Template with a Combo Box that uses Images

- there is no explicit DataTextField or DataValueField in the data items

it doesn't work at all!

the markup:

<telerik:GridTemplateColumn UniqueName="Target" DataField="Target" SortExpression="Target" HeaderText="Target" HeaderStyle-Width="40" >
    <FilterTemplate>
        <telerik:RadComboBox ID="rcbTarget" OnPreRender="rcbTarget_PreRender" OnSelectedIndexChanged="rcbTarget_SelectedIndexChanged" AutoPostBack="true" runat="server" />
    </FilterTemplate>
    <ItemTemplate>
        <asp:ImageButton ID="imgTarget" ImageUrl="../Images/tgt_neutral.png"  CommandName="SetTarget" CommandArgument='<%# Eval("Target") %>' Height="36" Width="36" runat="server" />
    </ItemTemplate>
</telerik:GridTemplateColumn>

 

the code behind:

    protected void rcbTarget_PreRender(object sender, EventArgs e)
    {
        RadComboBox rcbTarget;
        rcbTarget = sender as RadComboBox;
        if (ViewState["rcbTarget"] == null) { } else
        {
            rcbTarget.SelectedValue = ViewState["rcbTarget"].ToString();
        }
    }

    protected void rgDraftProspects_ItemCreated(object sender, GridItemEventArgs e)
    {
        GridFilteringItem theFilterItem;
        RadComboBox rcbTarget, rcbPager;

        if (e.Item is GridFilteringItem)
        {
            theFilterItem = e.Item as GridFilteringItem;
            //set dimensions for the filters

            rcbTarget = theFilterItem["Target"].Controls[1] as RadComboBox;
            FillTargetFilter(rcbTarget);
        }

    }

    private void FillTargetFilter(RadComboBox rcbTarget)
    {
        RadComboBoxItem rcbiTarget;

        rcbiTarget = new RadComboBoxItem();
        rcbiTarget.Text = "No Filter";
        rcbiTarget.Value = string.Empty;
        rcbTarget.Items.Add(rcbiTarget);
//      rcbiTarget = new RadComboBoxItem();
//      rcbiTarget.ImageUrl = "../Images/tgt_neutral.png";
//      rcbiTarget.Value = DBNull.Value.ToString();
//      rcbTarget.Items.Add(rcbiTarget);
        rcbiTarget = new RadComboBoxItem();
        rcbiTarget.ImageUrl = "../Images/tgt_yes.png";
        rcbiTarget.Value = "1";
        rcbTarget.Items.Add(rcbiTarget);
        rcbiTarget = new RadComboBoxItem();
        rcbiTarget.ImageUrl = "../Images/tgt_no.png";
        rcbiTarget.Value = "0";
        rcbTarget.Items.Add(rcbiTarget);
    }

    protected void rcbTarget_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        StringBuilder sb;

        ViewState["rcbTarget"] = e.Value;
        sb = new StringBuilder();
        sb.Append("([Target] = '");
        sb.Append(e.Value);
        sb.Append("') ");
        rgDraftProspects.MasterTableView.FilterExpression = sb.ToString(); ;
        rgDraftProspects.MasterTableView.Rebind();
    }

 

6 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 14 Mar 2018, 08:16 AM
Hello Chris,

I've replied to your formal support ticket about this matter. I suggest that we continue our technical conversation on the mentioned thread.

Regards,
Eyup
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Chris
Top achievements
Rank 1
answered on 25 Mar 2018, 12:00 PM

I got it to work by changing the data access from server-side NeedDataSource event to SQLDataSource - even though the back end uses Entity Framework that particular page uses a Stored Procedure - no need to add any more layers of calls

with that a lot just fell into place

my code is on another machine so I'll add it later today (EDT)

0
Chris
Top achievements
Rank 1
answered on 26 Mar 2018, 05:33 PM

I extended the GridTemplateColumn

                    <custom:CustomTemplateColumn UniqueName="Target" DataField="Target" AllowSorting="true" SortExpression="Target" HeaderText="Target" HeaderStyle-Width="64" >
                        <ItemTemplate>
                            <asp:ImageButton ID="imgTarget" ImageUrl="../Images/tgt_neutral.png"  CommandName="SetTarget" CommandArgument='<%# Eval("Target") %>' Height="36" Width="36" runat="server" />
                        </ItemTemplate>
                    </custom:CustomTemplateColumn>

used a SQLDataSource

            <asp:SqlDataSource ID="sdsDraftProspect" ConnectionString="<%$ ConnectionStrings:PowerHouseUltimateConnectionString %>"
                SelectCommandType="StoredProcedure" SelectCommand="LoadDraftProspects" runat="server" >
                <SelectParameters>
                    <asp:ControlParameter Name="year" ControlID="rcbSeason" DbType="Int16" PropertyName="SelectedValue" />
                    <asp:Parameter Name="memberid" Type="Int32" />
                </SelectParameters>
            </asp:SqlDataSource>

 

created a class in AppCode (the client wanted literals instead of images but it would not have been much different)

using System;
using System.Data;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

/// <summary>
/// Summary description for CustomRadgridFilter
/// </summary>
namespace PowerHouse.UltimateWebSite
{
    public class CustomTemplateColumn : GridTemplateColumn
    {
        private object listOfChoices = null;
        private int? aSelectedValue = null;
        private SqlDataSource sdsDraftProspect = null;

        public CustomTemplateColumn()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        protected override void SetupFilterControls(TableCell TCell)
        {
            DropDownList AListofItems;

            base.SetupFilterControls(TCell);
            TCell.Controls.RemoveAt(0);
            AListofItems = new DropDownList();
            AListofItems.ID = "ddl" + this.DataField;
            AListofItems.AutoPostBack = true;
            AListofItems.SelectedIndexChanged += new EventHandler(AListofItems_SelectedIndexChanged);
            TCell.Controls.AddAt(0, AListofItems);
            TCell.Controls.RemoveAt(1);
            AListofItems.DataTextField = this.DataField + "Name";
            AListofItems.DataValueField = this.DataField;
            AListofItems.DataSource = GettheChoices(this.DataField);
            if (sdsDraftProspect == null) { }
            else
            {
                if (sdsDraftProspect.SelectParameters["targetFilter"] == null) { }
                else
                {
                    AListofItems.SelectedValue = sdsDraftProspect.SelectParameters["targetFilter"].DefaultValue;
                }
            }
        }

        private DataTable GettheChoices(string theDataField)
        {
            DataTable dt;
            DataColumn dc;
            DataRow dr;

            if (theDataField == "Target")
            {
                dt = new DataTable();
                dc = new DataColumn("TargetName", typeof(System.String));
                dt.Columns.Add(dc);
                dc = new DataColumn("Target", typeof(System.Int32));
                dt.Columns.Add(dc);
                dr = dt.NewRow();
                dr[0] = "No Filter";
                dr[1] = DBNull.Value;
                dt.Rows.Add(dr);
                dr = dt.NewRow();
                dr[0] = "Target";
                dr[1] = 1;
                dt.Rows.Add(dr);
                dr = dt.NewRow();
                dr[0] = "Avoid";
                dr[1] = 0;
                dt.Rows.Add(dr);
                return dt;
            }
            else { return (DataTable)null; }
        }

        protected void AListofItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList theSelections;
            GridFilteringItem aFilterItem;
            GridColumn theColumn;
            Parameter aParameter;

            theSelections = (DropDownList)sender;
            aFilterItem = theSelections.NamingContainer as GridFilteringItem;
            theColumn = (GridColumn) aFilterItem.OwnerTableView.GetColumn("Target");
            if (sdsDraftProspect == null)
            {
                return;
            }
            aParameter = sdsDraftProspect.SelectParameters["targetFilter"];
            if (theSelections.SelectedItem.Value.Length < 1)
            {
                if (aParameter == null) { } else
                {
                    sdsDraftProspect.SelectParameters.Remove(aParameter);
                }
            }
            else
            {
                aSelectedValue = Convert.ToInt32(theSelections.SelectedValue);
                if (aParameter == null)
                {
                    sdsDraftProspect.SelectParameters.Add("targetFilter", aSelectedValue.ToString());
                }
                else
                {
                    aParameter.DefaultValue = aSelectedValue.ToString();
                }
            }
        }

        //RadGrid calls this method when the value should be set to the filtering input control(s)  
        protected override void SetCurrentFilterValueToControl(TableCell tCell)
        {
            DropDownList theList;

            base.SetCurrentFilterValueToControl(tCell);
            theList = (DropDownList)tCell.Controls[0];
            if (this.CurrentFilterValue == string.Empty) { }
            else
            {
                theList.SelectedValue = this.CurrentFilterValue;
            }
        }

        //RadGrid calls this method to extract the filtering value from the filtering input control(s)  
        protected override string GetCurrentFilterValueFromControl(TableCell TCell)
        {
            DropDownList theList;

            theList = (DropDownList)TCell.Controls[0];
            return theList.SelectedValue;
        }

        protected override string GetFilterDataField()
        {
            return this.DataField;
        }

        public object AlltheChoices
        {
            get
            {
                return this.listOfChoices;
            }
            set
            {
                listOfChoices = value;
            }
        }

        public int? theSelectedValue
        {
            get
            {
                return this.aSelectedValue;
            }
        }

        public SqlDataSource theDatasource
        {
            set
            {
                sdsDraftProspect = value;
            }
        }
    }
}

 

in the ItemCommand event handler:

        if (e.CommandName == "SetTarget")
        {
            theItem = (GridDataItem)e.Item;
            ProspectID = (int)theItem.GetDataKeyValue("prospectId");
            if (e.CommandArgument.ToString().Length < 1)
            {
                blnTarget = true;
            }
            else
            {
                if (e.CommandArgument.ToString() == "True")
                {
                    blnTarget = false;
                }
                else
                {
                    blnTarget = null;
                }
            }

            DraftProspectService.UpdateMemberTarget(MemberID, ProspectID, blnTarget);

0
Chris
Top achievements
Rank 1
answered on 26 Mar 2018, 05:38 PM

in the Page Load drop in the address of the data source

        for (i = 0; i < rgDraftProspects.Columns.Count; i++)
        {    
            if (rgDraftProspects.Columns[i] is CustomTemplateColumn)
            {
                anotherColumn = (CustomTemplateColumn)rgDraftProspects.Columns[i];
                anotherColumn.theDatasource = sdsDraftProspect;
            }
        }

0
Chris
Top achievements
Rank 1
answered on 26 Mar 2018, 05:40 PM

in the Page Load

        for (i = 0; i < rgDraftProspects.Columns.Count; i++)
        {
              if (rgDraftProspects.Columns[i] is CustomTemplateColumn)
            {
                anotherColumn = (CustomTemplateColumn)rgDraftProspects.Columns[i];
                anotherColumn.theDatasource = sdsDraftProspect;
            }
        }

0
Accepted
Eyup
Telerik team
answered on 28 Mar 2018, 08:40 PM
Hi Chris,

Thank you sharing your specific implementation with our community. I hope it helps other developers as well.

Regards,
Eyup
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Chris
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Chris
Top achievements
Rank 1
Share this question
or