Error with AutoCompleteBox inside asp:Repeater

2 posts, 0 answers
  1. Daniel
    Daniel avatar
    3 posts
    Member since:
    Nov 2017

    Posted 28 Jan Link to this post

    Hello,

     

    I have a page that I'm building that uses a AutoCompleteBox inside of a repeater.  I am trying to load the control inside of the the ItemDataBound method of the repeater, but an error gets thrown before it can even be called saying "Datasource not set" referring to the AutoCompleteBox.  Is there a specific way that I should be loading the control?  Here is a snippet of the control being loaded inside the ItemDataBound method of the repeater.  Thanks for any help.

     

    {

         RepeaterItem item = e.item;

     

        txtAgent = (RadAutoCompleteBox)item.FindControl("txtAgent");

        txtAgent.DataSource  = Agent.GetListOfAgents();

        txtAgent.DataBind();

    }

  2. Peter Milchev
    Admin
    Peter Milchev avatar
    426 posts

    Posted 31 Jan Link to this post

    Hello Daniel,

    TData bound controls call the .DataBind() method of their items between the ItemCreated and ItemDataBound event. This causes the autocomplete box to data bind. Since it has no data source set yet, it throws an exception.

    More information can be found in the DataSource not set error thrown when data source is provided in ItemDataBound KB article.

    <asp:Repeater ID="Repeater1" runat="server"
        DataSourceID="SqlDataSource1" OnItemCreated="Repeater1_ItemCreated" >
        <ItemTemplate>
            <telerik:RadAutoCompleteBox ID="RadAutoCompleteBox1" runat="server"></telerik:RadAutoCompleteBox>
            <br />
        </ItemTemplate>
    </asp:Repeater>
     
    <asp:SqlDataSource
        ConnectionString=
            "<%$ ConnectionStrings:NorthwindConnectionString %>"
        ID="SqlDataSource1" runat="server"
        SelectCommand="SELECT [CategoryID], [CategoryName],
            [Description] FROM [Categories]">
    </asp:SqlDataSource>
    protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        RepeaterItem item = e.Item;
     
        string[] ds = Page.IsCallback ? new string[] { "aaa", "bbb", "ccc", "ddd", "sss" } : new string[] { "" };
     
        var txtAgent = (RadAutoCompleteBox)item.FindControl("RadAutoCompleteBox1");
        txtAgent.DataSource = ds;
    }

    Let us know how that works for you or if you have any feedback or improvement suggestions regarding the KB article.

    Regards,
    Peter Milchev
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top