1

I have a visual web part that works if you insert it directly into the page layout, but not when you add it to a web part zone.

The page:

<asp:UpdatePanel ID="UPnlArticle" runat="server">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="navPrev" EventName="click" />
            <asp:AsyncPostBackTrigger ControlID="navNext" EventName="click" />
        </Triggers>
        <ContentTemplate>
            <asp:Literal id="litNoResults" runat="server"></asp:Literal>
            <asp:Repeater id="repResults" runat="server">
            <ItemTemplate>
                <div class="current-vacancies-item">
                    <div style="float:right;font-size:10px"><%#((DateTime)Eval("ArticleStartDate")).ToString("d MMM yy")%></div>
                    <p><a href="/Communications/Pages/<%#Eval("FileLeafRef")%>"><%#Eval("ArticleLeadParagraph")%></a></p>
                </div>
            </ItemTemplate>
            </asp:Repeater>

            <asp:Literal id="litError" runat="server"></asp:Literal>
            <div class="footer">
                <asp:LinkButton ID="navPrev" runat="server" Text="Previous" 
                    onclick="navPrev_Click"></asp:LinkButton>
                <asp:Literal ID="litNav" runat="server"></asp:Literal>
                <asp:LinkButton ID="navNext" runat="server" Text="Next" onclick="navNext_Click" ></asp:LinkButton>
            </div>

            <asp:HiddenField ID="hidPageNo" runat="server" Value="0" />
        </ContentTemplate>
    </asp:UpdatePanel>   

The code behind:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadCurrentVacancies();
        }
    }



    private void LoadCurrentVacancies()
    {
        try
        {
            int PageNum = Convert.ToInt32(hidPageNo.Value);

            using (SPSite siteCollection = new SPSite("http://oursite"))
            {
                using (SPWeb webSite = siteCollection.OpenWeb("OurWeb"))
                {
                    SPList list = webSite.GetList("/theURL");
                    SPQuery query = new SPQuery();

                    query.Query = "<Where><And><And><And><And><Eq><FieldRef Name='ContentType' /><Value Type='Text'>EINewsArticle</Value></Eq><Eq><FieldRef Name='_Level' /><Value Type='Integer'>1</Value></Eq></And><Leq><FieldRef Name='ArticleStartDate' /><Value Type='DateTime'>#TodayDate#</Value></Leq></And><Gt><FieldRef Name='EIExpiryDate' /><Value IncludeTimeValue='TRUE' Type='DateTime'>#TodayDate#</Value></Gt></And><Eq><FieldRef Name='EIArticleCategory' /><Value Type='Choice'>Vacancy</Value></Eq></And></Where><OrderBy><FieldRef Name='ArticleStartDate' Ascending='False' /><FieldRef Name='Modified' Ascending='False' /></OrderBy>";
                    string dtToday = SPUtility.CreateISO8601DateTimeFromSystemDateTime(System.DateTime.Now);

                    query.Query = query.Query.Replace("#TodayDate#", dtToday);

                    SPListItemCollection spColl = list.GetItems(query);

                    if (spColl.Count != 0)
                    {
                        System.Data.DataTable table = new System.Data.DataTable();
                        table = spColl.GetDataTable();
                        table.AcceptChanges();

                        PagedDataSource objPds = new PagedDataSource();
                        objPds.DataSource = table.DefaultView;
                        objPds.AllowPaging = true;

                        objPds.PageSize = 5;

                        objPds.CurrentPageIndex = PageNum;

                        litNav.Text = "Page: " + (PageNum + 1).ToString() + " of " + objPds.PageCount.ToString();

                        navPrev.Enabled = !objPds.IsFirstPage;
                        navNext.Enabled = !objPds.IsLastPage; 

                        repResults.DataSource = objPds;
                        repResults.DataBind();
                    }

                    else
                    {
                        litNoResults.Text = "<div style = 'text-align:center; padding-top:50px; font-size:14px;'>There are no current vacancies</div>";
                        navPrev.Visible = false;
                        navNext.Visible = false;
                    }

                }
            }
        }
        catch (Exception ex)
        {
            litError.Text = ex.Message;
        }
    }

    protected void navPrev_Click(object sender, EventArgs e)
    {
        // Set viewstate variable to the previous page
        int PageNum = Convert.ToInt32(hidPageNo.Value);
        hidPageNo.Value = (PageNum - 1).ToString();

        // Reload control
        LoadCurrentVacancies();
    }

    protected void navNext_Click(object sender, EventArgs e)
    {
        litNoResults.Text = "CLick event fired";

        // Set viewstate variable to the next page
        int PageNum = Convert.ToInt32(hidPageNo.Value);
        hidPageNo.Value = (PageNum + 1).ToString();

        // Reload control
        LoadCurrentVacancies();
    }

It brings back the list of vacancies as it should. The problem is that the paging controls do not work.

Is there a way to make this work within a web part zone? Do I need to override the webpart OnInit method?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.