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

2 posts, 0 answers
  1. toddhd
    toddhd avatar
    21 posts
    Member since:
    Aug 2006

    Posted 16 Feb 2011 Link to this post

    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?

  2. Helen
    Admin
    Helen avatar
    1052 posts

    Posted 18 Feb 2011 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top