Tags

, ,

Listview and Datapager are new controls in ASP .Net 3.5, Listview is similar as Repeater, Datapager can paging your listview for you. As other data-aware controls works, you need data-source control.

                    <asp:ListView ID=”SearchResults” OnItemDataBound=”SearchResultBound” 
                    datasourceid=”KnowledgeEntryDataSource”    runat=”server”>
                        <LayoutTemplate>
                          <table class=”search_grid” cellpadding=”0″ cellspacing=”0″>
                            <tr id=”Tr1″ style=”background-color: #ADD8E6″ runat=”server”>
                                <th class=”col1″ runat=”server”>Date</th>
                                <th class=”col2″ runat=”server”>Author</th>
                                <th class=”col3″ runat=”server”>Title</th>
                            </tr>
                            <tr runat=”server” id=”itemPlaceholder” />
                          </table>
                          <div class=”blockheader” style=”padding:10px;text-align: right;”>
                            <asp:DataPager runat=”server” ID=”ItemDataPager” PageSize=”12″ PagedControlID=”SearchResults”>
                                <Fields>
                            <asp:TemplatePagerField>
                            <PagerTemplate>
                            <b>Page
                            <asp:Label runat=”server” ID=”CurrentPageLabel”
                            Text=”<%# Container.TotalRowCount>0 ? (Container.StartRowIndex / Container.PageSize) + 1 : 0 %>” />
                            of
                            <asp:Label runat=”server” ID=”TotalPagesLabel”
                            Text=”<%# Math.Ceiling ((double)Container.TotalRowCount / Container.PageSize) %>” />
                            (<asp:Label runat=”server” ID=”TotalItemsLabel” Text=”<%# Container.TotalRowCount%>” />
                            records)
                            <br />
                            </b>
                            </PagerTemplate>
                            </asp:TemplatePagerField>
                            <asp:NextPreviousPagerField ButtonType=”Button” ShowFirstPageButton=”true”
                            ShowNextPageButton=”false” ShowPreviousPageButton=”false” />
                            <asp:NumericPagerField PreviousPageText=”< Prev 2″ NextPageText=”Next 2 >”
                            ButtonCount=”10″ />
                            <asp:NextPreviousPagerField ButtonType=”Button” ShowLastPageButton=”true”
                            ShowNextPageButton=”false” ShowPreviousPageButton=”false” />
                            </Fields>
                            </asp:DataPager>
                          </div>
                        </LayoutTemplate>

                        <ItemTemplate>
                           
                                <tr<asp:Literal ID=”Row” runat=”server” />>
                                    <td class=”col1″><asp:Literal ID=”Date” runat=”server” /></td>
                                    <td class=”col2″><asp:Literal ID=”Author” runat=”server” /></td>
                                    <td class=”col3″><asp:Literal ID=”Title” runat=”server” /></td>
                                </tr>
                        </ItemTemplate>
                        <EmptyDataTemplate>
                            <div>
                            <P>Sorry, but there were no search results </P>
                            </div>
                        </EmptyDataTemplate>
                    </asp:ListView>

    <asp:ObjectDataSource ID=”KnowledgeEntryDataSource” typename=”KnowledgeEntryLogic”
    selectmethod=”GetSearchResults” OnSelecting=”KnowledgeEntryDataSource_Selecting”
    runat=”server”>
     <SelectParameters>
        <asp:Parameter Name=”MSSQLQuery” Type=”String” />
    </SelectParameters>
    </asp:ObjectDataSource>

In your code behind class, you define databound and select event

        protected void SearchResultBound(Object sender, ListViewItemEventArgs e)
        {
            ListViewDataItem dataItem = (ListViewDataItem)e.Item;
            KnowledgeEntry entry = dataItem.DataItem as KnowledgeEntry;

            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                Literal rowLiteral = e.Item.FindControl(“Row”) as Literal;
                Literal dateLiteral = e.Item.FindControl(“Date”) as Literal;
                Literal authorLiteral = e.Item.FindControl(“Author”) as Literal;
                Literal titleLiteral = e.Item.FindControl(“Title”) as Literal;
                if (rowLiteral != null && dateLiteral != null && authorLiteral != null && titleLiteral != null)
                {
                    if (IsFirstSearchResult(entry))
                    {
                        rowLiteral.Text = @” class=””first”””;
                    }
                    else if (IsLastSearchResult(entry))
                    {
                        rowLiteral.Text = @” class=””last”””;
                    }
                    dateLiteral.Text = PresentDate(entry.Date);
                    authorLiteral.Text = entry.Author;
                    //titleLiteral.Text = @”<a href=””” + drv[“Path”].ToString() + @”””>” + drv[ “Title”].ToString() + “</a>”;
                    titleLiteral.Text = @”<a href=””” + entry.Link + @”””>” + entry.Title + “</a>”;
                }
            }
        }

        protected void KnowledgeEntryDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {

            e.InputParameters[“MSSQLQuery”] = String.Format(“SELECT Title, Rank, Size, Description, Write, Path, Author,Created  FROM SCOPE() WHERE FREETEXT(‘{0}’) –”
                                              , _Query);
        }

 

Advertisements