Get the RadComboBox in CommandItemTemplate on Page_Load

2 posts, 0 answers
  1. Ali
    Ali avatar
    24 posts
    Member since:
    Apr 2010

    Posted 22 Nov 2014 Link to this post

    Hi,
    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;
                }
        
            } 

        ------------------------------------------------------------------------------

  2. Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 26 Nov 2014 Link to this post

    Hello,

    To achieve the desired functionality you need to execute the code on RadGrid’s PreRender event instead of on PageLoad and to call RadGrid’s Rebind() method. I am sending you a simple example based on your code which demonstrates the desired functionality.

    I hope this helps.  

    Regards,
    Radoslav
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top