?
Solved

How can I find my link button in my <PagerTemplate>

Posted on 2011-10-10
9
Medium Priority
?
782 Views
Last Modified: 2012-05-12
I've got a <PagerTemplate> within my GridView control. In this template, i'm trying to access a LinkButton, that lives in the pager template but am continuously coming up with a null reference acception. How can I find my link button in my <PagerTemplate>, within my GridView? See attached code snippets below...
markup (HTML)
---------------------------------------------------------------
<asp:UpdatePanel runat="server" ID="upStudentLoginGrid" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:GridView ID="gdvListOfStudents" runat="server" AutoGenerateColumns="false" CssClass="SignedInGrid"
            GridLines="None" AllowSorting="true" AllowPaging="true" PageSize="10" PageIndex="0" OnPageIndexChanging="gdvListOfStudents_OnPageIndexChanging"
            PagerStyle-HorizontalAlign="Center" OnRowCommand="gdvListOfStudents_OnRowCommand" DataKeyNames="AthleteUid">
            <Columns>
            .
            .
            .
            </Columns>
            <PagerSettings Mode="NextPrevious" />
            <PagerStyle CssClass="Paging" />
            <PagerTemplate>
                <asp:LinkButton ID="PreviousLinkButton" runat="server" CommandArgument="Prev" CommandName="Page"
                    CssClass="PagerControl" />
                <asp:Label ID="PageDescriptionLabel" runat="server" Style="display: inline" CssClass="PagerControl" />
                <asp:LinkButton ID="NextLinkButton" runat="server" CommandArgument="Next" CommandName="Page"
                    CssClass="PagerControl" />
            </PagerTemplate>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>


code behind (C#)
------------------------------------------------------------
    private void BindData(StudentListBaseList studentList)
    {
        int pageSize = gdvListOfStudents.PageSize;
        int currentPage = gdvListOfStudents.PageIndex;
        int lastPage = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(studentList.Count / gdvListOfStudents.PageSize)));
        int maxCount = ((currentPage + 1) * gdvListOfStudents.PageSize);
        if (maxCount > studentList.Count) maxCount = studentList.Count;

        gdvListOfStudents.DataSource = studentList;
        gdvListOfStudents.DataBind();

        LinkButton prevLinkButton = gdvListOfStudents.BottomPagerRow.FindControl("PreviousLinkButton") as LinkButton; //<--HERE IS WHERE I GET THE NULL REFFERENCE
        LinkButton nextLinkButton = gdvListOfStudents.BottomPagerRow.FindControl("NextLinkButton") as LinkButton;
        Label pageDescriptionLabel = gdvListOfStudents.BottomPagerRow.FindControl("PageDescriptionLabel") as Label;
        prevLinkButton.Text = "< prev " + pageSize.ToString();
        nextLinkButton.Text = "next " + pageSize.ToString() + " >";


        prevLinkButton.Enabled = (currentPage > 0);
        nextLinkButton.Enabled = (currentPage < lastPage);
        pageDescriptionLabel.Text = String.Format("{0} to {1} of {2}", (currentPage * pageSize) + 1, maxCount, studentList.Count);


        gdvListOfStudents.Visible = true;
    }

Open in new window

0
Comment
Question by:Michael Sterling
  • 5
  • 3
9 Comments
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36946711
Try Below code
  LinkButton prevLinkButton = (LinkButton )gdvListOfStudents.BottomPagerRow.Cells[0].FindControl("PreviousLinkButton");
 LinkButton nextLinkButton = (LinkButton )gdvListOfStudents.BottomPagerRow.Cells[0].FindControl("NextLinkButton");
        Label pageDescriptionLabel = (Label)gdvListOfStudents.BottomPagerRow.Cells[0].FindControl("PageDescriptionLabel");
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 36946772
@ramkisan: still getting null reference...
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36946816
How many records are there in your gidview? are they less than 10? if yes try with adding records more than 10.
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 36946841
not sure I understand. There are more than 10 records though...
0
 
LVL 12

Accepted Solution

by:
Ramkisan Jagtap earned 2000 total points
ID: 36946918
Hey,
I dont know why are u getting this error, I have tested with code below and its working fine

 <asp:GridView ID="gdvListOfStudents" runat="server" AutoGenerateColumns="False" CssClass="SignedInGrid"
            GridLines="None" AllowSorting="True" AllowPaging="True" OnPageIndexChanging="gdvListOfStudents_OnPageIndexChanging"
            PagerStyle-HorizontalAlign="Center"
        OnRowCommand="gdvListOfStudents_OnRowCommand" DataKeyNames="AthleteUid">
            <Columns>          
                <asp:BoundField HeaderText="Number" DataField="AthleteUid" />                          
            </Columns>
            <PagerSettings Mode="NextPrevious" />
            <PagerStyle CssClass="Paging" />
            <PagerTemplate>
                <asp:LinkButton ID="PreviousLinkButton" runat="server" CommandArgument="Prev" CommandName="Page"
                    CssClass="PagerControl" />
                <asp:Label ID="PageDescriptionLabel" runat="server" Style="display: inline" CssClass="PagerControl" />
                <asp:LinkButton ID="NextLinkButton" runat="server" CommandArgument="Next" CommandName="Page"
                    CssClass="PagerControl" />
            </PagerTemplate>
        </asp:GridView>



protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindGridview();

            LinkButton prevLinkButton = gdvListOfStudents.BottomPagerRow.FindControl("PreviousLinkButton") as LinkButton;
            LinkButton nextLinkButton = gdvListOfStudents.BottomPagerRow.FindControl("NextLinkButton") as LinkButton;
            Label pageDescriptionLabel = gdvListOfStudents.BottomPagerRow.FindControl("PageDescriptionLabel") as Label;
        }

        protected void BindGridview()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("AthleteUid");
            dt.Rows.Add(new object[] { 1  });
            dt.Rows.Add(new object[] {2  });
            dt.Rows.Add(new object[] { 3  });
            dt.Rows.Add(new object[] { 4 });
            dt.Rows.Add(new object[] { 5  });
            dt.Rows.Add(new object[] { 6  });
            dt.Rows.Add(new object[] { 7  });
            dt.Rows.Add(new object[] { 8  });
            dt.Rows.Add(new object[] { 9  });
            dt.Rows.Add(new object[] { 10  });
            dt.Rows.Add(new object[] { 11 });
            dt.Rows.Add(new object[] { 12  });
            dt.Rows.Add(new object[] { 13 });
            dt.Rows.Add(new object[] { 14 });
            dt.Rows.Add(new object[] { 15 });
            gdvListOfStudents.DataSource = dt;
            gdvListOfStudents.DataBind();
        }

please comare if anything is wrong

Thanks
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 36949966
does it matter or will it affect my efforts to find my controls if the Gridview control is wrapped in a panel or an update panel? just curious...
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 36950519
this grid lives on / in a usercontrol that get's databound when the user clicks a button on the parent form that contains the user control. not sure if that helps...i am trying your code unsuccessfully so far. still messing with it...
0
 
LVL 1

Author Closing Comment

by:Michael Sterling
ID: 36951289
thank you.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question