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

How to reset the SelectedIndex of a Combobox with an ItemTemplate?

1 Answer 99 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
toddhd
Top achievements
Rank 1
toddhd asked on 16 Feb 2011, 08:40 PM
I have a RadComboBox with an ItemTemplate for checkboxes, like this:

<li>
    <label class="labelBlack" for="ReportType">
        Report Type</label>
    <telerik:RadComboBox ID="cmbReportType" Name="ReportType" runat="server" DataTextField="Value" DataValueField="Key" TabIndex="17" Width="214"
         MarkFirstMatch="true" AllowCustomText="false" OnClientLoad="OnReportTypeLoad" OnClientDropDownClosing="OnReportTypeDropDownClosing">
        <ItemTemplate>
            <asp:CheckBox runat="server" ID="chkReportType" Text='<%# Eval("Value")%>'/>
            <asp:HiddenField runat="server" ID="ReportTypeID" Value='<%# Eval("Key") %>' />
        </ItemTemplate>
    </telerik:RadComboBox>
</li>
This is part of a "Search" form, and I have a Submit and Clear button on the page. When Clear is clicked, I need to set the index back to the first item in the list. On ComboBoxes that do not use an ItemTemplate, I can just do this (in code behind):
cmbReportType.SelectedIndex = 0;

And that works. But it doesn't work when the ItemTemplate is present. I also tried:

cmbReportType.Items[0].Selected = true;
Still, no luck. Any thoughts on this?

1 Answer, 1 is accepted

Sort by
0
Helen
Telerik team
answered on 18 Feb 2011, 03:31 PM
Hi Todd,

The following code works at our side:
//aspx:
<telerik:RadComboBox ID="cmbReportType" Name="ReportType" runat="server" TabIndex="17" Width="214"
 MarkFirstMatch="true" AllowCustomText="false">
<ItemTemplate>
    <asp:CheckBox runat="server" ID="chkReportType" Text='CheckBox>'/>
    <asp:HiddenField runat="server" ID="ReportTypeID" Value='Hidden' />
</ItemTemplate>
</telerik:RadComboBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Clear" />

//cs:
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string path = "combobox.xml";
 
            StreamReader sr = new StreamReader(Server.MapPath(path));
 
            //load items from XML string
            cmbReportType.LoadXml(sr.ReadToEnd());
            sr.Close();
        }
    }
protected void Button1_Click(object sender, EventArgs e)
{
    cmbReportType.SelectedIndex = 0;
}


Greetings,
Helen
the Telerik team
Tags
ComboBox
Asked by
toddhd
Top achievements
Rank 1
Answers by
Helen
Telerik team
Share this question
or