Get the RadComboBox in CommandItemTemplate on Page_Load

892 views Asked by At

I am trying to get the RadComboBox [Cmb_Lst_Sel] in CommandItemTemplate on Page_Load. Once I have the list ids [lst_ids] from the RadComboBox [Cmb_Lst_Sel], I pass these IDs to SqlDataSource to filter and rebind the RadGrid [Grd_Url]. But I get error and cannot get a reference to the RadComboBox [Cmb_Lst_Sel].

What am I doing wrong?

ASPX Code:
------------------------------------------------------------------------------
<telerik:RadGrid ID="Grd_Url" runat="server" GridLines="None" AllowSorting="true" AllowPaging="true" PageSize="50"
    AllowFilteringByColumn="true" AutoGenerateColumns="False" AllowMultiRowSelection="true" AllowMultiRowEdit="true"
    AllowAutomaticInserts="True" AllowAutomaticUpdates="True" AllowAutomaticDeletes="True" 
    DataSourceID="Sql_Url" Width="100%">
    <SortingSettings SortToolTip=""/>
    <FilterItemStyle Width="100%"></FilterItemStyle>
    <GroupingSettings CaseSensitive="false"></GroupingSettings>
    <PagerStyle AlwaysVisible="true" Mode="NextPrevAndNumeric"/>
    <MasterTableView DataKeyNames="url_id" CommandItemDisplay="Top" EditMode="InPlace" InsertItemPageIndexAction="ShowItemOnCurrentPage"
        GroupLoadMode="Client" GroupsDefaultExpanded="true" TableLayout="Fixed" Width="100%">
        <GroupByExpressions>
            <telerik:GridGroupByExpression>
                <SelectFields>
                    <telerik:GridGroupByField FieldName="lst_name"></telerik:GridGroupByField>
                    <telerik:GridGroupByField FieldName="lst_sort"></telerik:GridGroupByField>
                </SelectFields>
                <GroupByFields>
                    <telerik:GridGroupByField FieldName="lst_sort" SortOrder="Ascending"></telerik:GridGroupByField>
                    <telerik:GridGroupByField FieldName="lst_name" SortOrder="Ascending"></telerik:GridGroupByField>
                </GroupByFields>
            </telerik:GridGroupByExpression>
        </GroupByExpressions>
        <CommandItemTemplate>
        <div id="Div_Tlb_Fixed">
            <telerik:RadToolBar ID="Tlb_Url" runat="server" EnableImageSprites="true"
                OnButtonClick="CsTlbClick" OnClientButtonClicking="jsTlbUrl">
                <Items>
                    <telerik:RadToolBarButton CommandName="Toggle" PostBack="false" ToolTip="Expand/Collapse Folders"
                        CssClass="Btn_ToggleN" HoveredCssClass="Btn_ToggleH"
                        CheckOnClick="true" AllowSelfUnCheck="true" Group="T">
                    </telerik:RadToolBarButton>

                    <telerik:RadToolBarButton CommandName="Clear" PostBack="false" ToolTip="Clear Website Selection"
                        CssClass="Btn_ClearN" HoveredCssClass="Btn_ClearH">
                    </telerik:RadToolBarButton>
                </Items>
            </telerik:RadToolBar>
            <telerik:RadComboBox ID="Cmb_Lst_Sel" runat="server" DataTextField="lst_name" CheckBoxes="true"
                DataValueField="lst_id" AutoPostBack="True" EnableCheckAllItemsCheckBox="true" 
                DataSourceID="Sql_Lst" Width="20%">
                <Items>
                    <telerik:RadComboBoxItem Text="All" Value="" Selected="true"></telerik:RadComboBoxItem>
                </Items>
            </telerik:RadComboBox> 
        </div>
        </CommandItemTemplate>
        <Columns>
            <%--Some Columns here--%>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

<asp:SqlDataSource ID="Sql_Lst" runat="server" OnSelecting="CsSqlSelectingCmb"
ConnectionString="<%$ ConnectionStrings:Con_Str %>"
    SelectCommand="SELECT [lst_id], [lst_name] 
                   FROM [t_Lists] 
                   WHERE [usr_id] = @usr_id 
                   ORDER BY [lst_sort]">
    <SelectParameters>
        <asp:Parameter Name="usr_id"/>
    </SelectParameters>
</asp:SqlDataSource>


C# Code:
------------------------------------------------------------------------------

    public partial class Cls_Url : System.Web.UI.UserControl
    {
        protected void Page_Load(object s, EventArgs e)
        {
            // Grab the RadComboBox in CommandItemTemplate on Page_Load
            CsSqlSelect(s, e);
        }

        protected void CsSqlSelect(object s, EventArgs e)
        {
            // I get error for the 2 lines below and cannot get a reference to the RadComboBox [Cmb_Lst_Sel]
            // What am I doing wrong?
            GridItem cmdItem = Grd_Url.MasterTableView.GetItems(GridItemType.CommandItem)[0];
            RadComboBox cmbLst = (RadComboBox)cmdItem.FindControl("Cmb_Lst_Sel");

            if (cmbLst.CheckedItems.Count > 0)
            {
                var items = cmbLst.CheckedItems;
                string lst_ids = "";

                foreach (var item in items)
                {
                    lst_ids += "'" + item.Value + "'" + ",";
                }

                lst_ids = lst_ids.Remove(lst_ids.Length - 1);
            }

            // Once I have the list ids [lst_ids] from the RadComboBox [Cmb_Lst_Sel], 
            // I pass these IDs to SqlDataSource to filter and rebind the RadGrid [Grd_Url]
        }

        protected void CsTlbClick(object s, RadToolBarEventArgs e)
        {
        }

        protected void CsSqlSelectingCmb(object s, SqlDataSourceCommandEventArgs e)
        {
            e.Command.Parameters["@usr_id"].Value = Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey;
        }

    } 

------------------------------------------------------------------------------
1

There are 1 answers

1
idants On

What i suggest is to add an event on the RadGrid Grd_Url OnItemCreated and than use your function.

    protected void Grd_Url_OnItemCreated(object sender,GridItemEventArgs e)
    {
       if(e.Item is GridEditableItem))
          CsSqlSelect(sender, e);
    }

and edit your function:

    protected void CsSqlSelect(object s, GridItemEventArgs e)
    {
        GridEditableItem cmdItem = e.Item as GridEditableItem;
        RadComboBox cmbLst = (RadComboBox)cmdItem.FindControl("Cmb_Lst_Sel");

        if (cmbLst.CheckedItems.Count > 0)
        {
            var items = cmbLst.CheckedItems;
            string lst_ids = "";

            foreach (var item in items)
            {
                lst_ids += "'" + item.Value + "'" + ",";
            }

            lst_ids = lst_ids.Remove(lst_ids.Length - 1);
        }

        // Once I have the list ids [lst_ids] from the RadComboBox [Cmb_Lst_Sel], 
        // I pass these IDs to SqlDataSource to filter and rebind the RadGrid [Grd_Url]
    }