在Page_Load上的CommandItemTemplate中获取RadComboBox

时间:2014-11-22 08:54:47

标签: c# asp.net telerik radgrid radcombobox

我试图在Page_Load上的CommandItemTemplate中获取RadComboBox [Cmb_Lst_Sel]。一旦我从RadComboBox [Cmb_Lst_Sel]获得列表ids [lst_ids],我将这些ID传递给SqlDataSource以过滤和重新绑定RadGrid [Grd_Url]。但我得到错误,无法获得RadComboBox [Cmb_Lst_Sel]的引用。

我做错了什么?

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 个答案:

答案 0 :(得分:0)

我建议在RadGrid Grd_Url OnItemCreated上添加一个事件,而不是使用你的函数。

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

并编辑你的功能:

    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]
    }