This is a migrated thread and some comments may be shown as answers.

How to get selectedvalue of RadComboBox in templated RadListView?

0 Answers 58 Views
ListView
This is a migrated thread and some comments may be shown as answers.
H. Baris
Top achievements
Rank 1
H. Baris asked on 06 Apr 2017, 12:12 AM

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.

No answers yet. Maybe you can help?

Tags
ListView
Asked by
H. Baris
Top achievements
Rank 1
Share this question
or