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

Correct use of NeedDataSource event

1 Answer 100 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Brian
Top achievements
Rank 1
Brian asked on 03 May 2012, 09:49 PM
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?

1 Answer, 1 is accepted

Sort by
0
Brian
Top achievements
Rank 1
answered on 03 May 2012, 10:19 PM
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.
Tags
ListView
Asked by
Brian
Top achievements
Rank 1
Answers by
Brian
Top achievements
Rank 1
Share this question
or