Correct use of NeedDataSource event

2 posts, 0 answers
  1. Brian
    Brian avatar
    12 posts
    Member since:
    Apr 2012

    Posted 03 May 2012 Link to this post

    I would like to know if my usage of the NeedDataSource event is correct.

    <telerik:RadListView ID="rlvResults" runat="server" AllowPaging="True"
        ItemPlaceholderID="SearchResultsHolder" DataKeyNames="Id" Skin="Web20"
        EnableEmbeddedSkins="False" OnNeedDataSource="rlvResults_NeedDataSource">
        <LayoutTemplate>
            <asp:Panel ID="SearchResultsHolder" runat="server" />
            <div style="clear: both" />
            <div>
                <telerik:RadDataPager ID="rdpSearchResultsPager" runat="server"
                    PagedControlID="rlvResults" PageSize="5">
                    <Fields>
                        <telerik:RadDataPagerButtonField FieldType="FirstPrev" />
                        <telerik:RadDataPagerButtonField FieldType="Numeric" />
                        <telerik:RadDataPagerButtonField FieldType="NextLast" />
                        <telerik:RadDataPagerPageSizeField PageSizeText="Page size: " />
                    </Fields>
                </telerik:RadDataPager>
            </div>
        </LayoutTemplate>
        ...
    </RadListView>

    code behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ...
        }
    }
     
    protected void rlvResults_NeedDataSource(object sender, Telerik.Web.UI.RadListViewNeedDataSourceEventArgs e)
    {
        if (Page.IsPostBack)
        {
            rlvResults.DataSource = GetData();
        }
    }
     
    protected void bSearch_Click(object sender, EventArgs e)
    {
        rlvResults.DataSource = GetData();
        rlvResults.DataBind();
    }

    This is a search page. When bSearch is clicked the results should be shown in rlvResults. On Page_Load!Page.IsPostBack  the listview should not display any items.

    I've added a check to only get data for the listview when it's a client postback event (paging, sorting, etc). Is this the correct usage for this event?
  2. Brian
    Brian avatar
    12 posts
    Member since:
    Apr 2012

    Posted 03 May 2012 Link to this post

    I've added another check in the NeedDataSource event.

    if ((Page.IsPostBack) && (e.RebindReason == RadListViewRebindReason.ExplicitRebind))

    This prevents calling GetData() twice (in rlvResults_NeedDataSource and in bSearch_Click) when clicking on the search button.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top