How to get selectedvalue of RadComboBox in templated RadListView?

1 posts, 0 answers
  1. H. Baris
    H. Baris avatar
    8 posts
    Member since:
    Feb 2011

    Posted 05 Apr Link to this post

    Hi,

    I have RadComboBoxes in a templated RadListView:

    <telerik:RadListView ID="RadListView_ProductDetails" runat="server" OnNeedDataSource="RadListView_ProductDetails_NeedDataSource" DataKeyNames="ID" ItemPlaceholderID="ProductDetailsItemContainer" OnItemCommand="RadListView_ProductDetails_ItemCommand" OnItemDataBound="RadListView_ProductDetails_ItemDataBound">
        <LayoutTemplate>
            <div style="clear: both; margin-bottom: 3px">
                <asp:PlaceHolder ID="ProductDetailsItemContainer" runat="server"></asp:PlaceHolder>
            </div>
        </LayoutTemplate>
        <ItemTemplate>
            <div class="productdetail">
                <div class="productdetailcolumn productdetailtitle"><%#Eval("ProductDetailTitle")%></div>
                <div class="productdetailcolumn productdetailoptions"><telerik:RadComboBox ID="RadComboBox_ProductDetail" ClientIDMode="Static" AllowCustomText="false" EnableTextSelection="false" runat="server" Width="200px" DataTextField="ProductDetailItemKey" DataValueField="ProductDetailItemValue"></telerik:RadComboBox></div>
                <div class="productdetailcolumn productdetailbutton addbutton"><telerik:RadButton ID="RadButton_AddProductDetail" runat="server" Text="Ekle" CommandName="Add"><Icon PrimaryIconCssClass="rbAdd" /></telerik:RadButton></div>
            </div>
        </ItemTemplate>
    </telerik:RadListView>

    On ItemDataBound, rename all RadCombobox ID's to unique one.

    protected void RadListView_ProductDetails_ItemDataBound(object sender, Telerik.Web.UI.RadListViewItemEventArgs e)
    {
        RadListViewDataItem rlvi = (RadListViewDataItem) (e.Item);
        Int32 iProductDetailTitleID = Convert.ToInt32(rlvi.GetDataKeyValue("ID").ToString());
        RadComboBox rcb = (RadComboBox) rlvi.FindControl("RadComboBox_ProductDetail");
        rcb.ClientIDMode = ClientIDMode.Static;
        rcb.ID = "RadComboBox_ProductDetail_" + iProductDetailTitleID;
    }

    Then add items to all comboboxes:

    RadListView_ProductDetails.Rebind();
    foreach (RadListViewDataItem rlvi in RadListView_ProductDetails.Items)
    {
        Int32 iProductDetailTitleID = Convert.ToInt32(rlvi.GetDataKeyValue("ID").ToString());
        RadComboBox rcb = (RadComboBox) rlvi.FindControl("RadComboBox_ProductDetail_" + iProductDetailTitleID);
        RadControlActions.FillRadComboBox(rcb, "SELECT PreviouslyUsedValue AS ItemText, PreviouslyUsedValue AS ItemValue FROM ProductDetailTitlesPreviousValues WHERE ProductDetailTitleID = " + iProductDetailTitleID + " ORDER BY PreviouslyUsedValue", "");
    }

     

    All OK to this point. 

    But when i hit save button, i can't access selected values of RadComboBoxes with this method, because RadComboBox is null:

    foreach (RadListViewDataItem rlvi in RadListView_ProductDetails.Items)
    {
        Int32 iProductDetailTitleID = Convert.ToInt32(rlvi.GetDataKeyValue("ID").ToString());
        RadComboBox rcb = (RadComboBox) (rlvi.FindControl("RadComboBox_ProductDetail_" + iProductDetailTitleID));
        String strProductDetail = rcb.SelectedValue
    }

    I get error "System.NullReferenceException: Object reference not set to an instance of an object."

    How can get new selected values of this comboboxes?

    Thanks.

Back to Top