locked
Cannot access ASP.NET control via script or codebehind - "The name 'DataPager1' does not exist in the current context." RRS feed

  • Question

  • User-1826179050 posted

    I have an ASP.NET Web Application that I am trying to create a page with a ListView control - using a DataPager to page through the results in the ListView. I want to include a DropDown allowing the users to choose how many rows appear on a page of the DataPager.

    To do this, I setup a simple table in SQL Server and setup a SQLDataSource to connect to it. I then drop a ListView on the page and configure the DataSource so that is simply Selects this table using the SQLDataSource. I then Configure the ListView and tell it to Enable Paging - selecting Numeric Pager. Now when I select Ok at this point, I get the following error:

    "Error invoking 'Configure ListView...'. Details: Exception has been thrown by the target of an invocation."

    But, if I tell it to Configure the ListView again, it works fine and it creates all the ASP.NET aspx code for the ListView.

    I add the asp code for the DropDown and setup its OnSelectedIndexChanged event and AutoPostBack to true.

    In the code behind file below - I have the line of code referencing DataPager1 uncommented out - so as it is below - it will break. When I comment that line out and set the DropDownList selected value to a temp int variable, I set a breakpoint there just to verify the code is being run and the variable is being set to the selected value in the dropdown. This works fine.

    At this point - everything works (with the code in the code behind referencing the DataPager1 commented out). I simplified the code and removed the AlternateRow, Edit, etc... templates to simplify the code. Also removed some styling, etc...

    Now, the problem is, whether I include a Script section in the aspx file, or use C# in the code behind file, when I try to reference DataPager1 it gets underlined in red and I get an error message of:

    "The name 'DataPager1' does not exist in the current context."

    I have found many instances of issues where the designer.cs file is not syncing and causes issues like this, but I have tried everything in regard to that. I've deleted the designer.cs file and converted the ASPX file to a web application to regenerate it. I've restarted VS2015, restarted the computer, etc... I have created completely new Web Form with Master Pages documents from scratch - they are all doing this same thing in relation to the DataPager control being created by the ListView configuration process. I can access the DropDown, the ListView, Labels, etc... - everything else on the Form, just not the DataPager.

    I have included the code for the aspx file, the code behind, and the DDL with data for the data source below for review.

    I have modified the code to remove references to my namespaces, datasources, etc... So fix these for your testing environment as needed.

    TestingDataPagerAccess.aspx File:
    
    
    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="TestingDataPagerAccess.aspx.cs" Inherits="CHANGE THIS FOR YOUR PROJECT" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
        <asp:SqlDataSource ID="DataSourceBusinessFact" runat="server"
            ConnectionString="<%$ ConnectionStrings:SET THIS TO YOUR CONNECTION STRING FROM WEB.CONFIG FILE %>"        
            SelectCommand="SELECT * FROM [Subdivision_Dim]">        
        </asp:SqlDataSource>
    
        <asp:DropDownList runat="server" id="ResultsList" 
             OnSelectedIndexChanged="ResultsList_SelectedIndexChanged" 
             AutoPostBack="true">              
            <asp:ListItem Value="10"  />
            <asp:ListItem Value="25" Selected="True" />
            <asp:ListItem Value="50" />
        </asp:DropDownList>
    
        <asp:ListView ID="ListView1" runat="server" DataSourceID="DataSourceBusinessFact" DataKeyNames="SubdivisionID">               
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:Label Text='<%# Eval("SubdivisionID") %>' runat="server" ID="SubdivisionIDLabel" /></td>
                    <td>
                        <asp:Label Text='<%# Eval("SubdivisionName") %>' runat="server" ID="SubdivisionNameLabel" /></td>
                    <td>
                        <asp:CheckBox Checked='<%# Eval("Active") %>' runat="server" ID="ActiveCheckBox" Enabled="false" /></td>
                    <td>
                        <asp:Label Text='<%# Eval("Comment") %>' runat="server" ID="CommentLabel" /></td>
                </tr>
            </ItemTemplate>
            <LayoutTemplate>
                <table runat="server">
                    <tr runat="server">
                        <td runat="server">
                            <table runat="server" id="itemPlaceholderContainer" border="1">
                                <tr runat="server">
                                    <th runat="server">SubdivisionID</th>
                                    <th runat="server">SubdivisionName</th>
                                    <th runat="server">Active</th>
                                    <th runat="server">Comment</th>
                                </tr>
                                <tr runat="server" id="itemPlaceholder"></tr>
                            </table>
                        </td>
                    </tr>
                    <tr runat="server">
                        <td runat="server" style="text-align: center; background-color: #5D7B9D; color: #FFFFFF">
                            <asp:DataPager runat="server" ID="DataPager1" PageSize="10">
                                <Fields>
                                    <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="True" ShowPreviousPageButton="False"></asp:NextPreviousPagerField>
                                    <asp:NumericPagerField></asp:NumericPagerField>
                                    <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="True"></asp:NextPreviousPagerField>
                                </Fields>
                            </asp:DataPager>
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
        </asp:ListView>
    </asp:Content>
    
    TestingDataPagerAccess.aspx.cs - Code Behind File:
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace //Change this for your project
    {
        public partial class TestingDataPagerAccess : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void ResultsList_SelectedIndexChanged(object sender, EventArgs e)
            {
                // Set the PageSize of the DataPager to the value the user selected from the DropDownList
                DataPager1.PageSize = Convert.ToInt32(ResultsList.SelectedValue);  //Does not Work - cannot 'see' DataPager1
                int temp = Convert.ToInt32(ResultsList.SelectedValue);  // This works
            }
        }
    }
    DDL and Data for Test Table:
    
    USE [SET YOUR TEST DATABASE HERE]
    GO
    /****** Object:  Table [dbo].[Subdivision_Dim]    Script Date: 7/29/2016 9:33:55 AM ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TABLE [dbo].[Subdivision_Dim](
    	[SubdivisionID] [int] IDENTITY(1,1) NOT NULL,
    	[SubdivisionName] [nvarchar](50) NOT NULL,
    	[Active] [bit] NOT NULL CONSTRAINT [DF_Subdivision_Dim_Active]  DEFAULT ((1)),
    	[Comment] [nvarchar](255) NOT NULL,
     CONSTRAINT [PK_Subdivision_Dim] PRIMARY KEY CLUSTERED 
    (
    	[SubdivisionID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    GO
    SET IDENTITY_INSERT [dbo].[Subdivision_Dim] ON 
    
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (1, N'None', 1, N'Used when no subdivision is associated with the referencing record')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (2, N'DVC', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (3, N'BBW', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (4, N'YR', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (5, N'BRCAL', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (6, N'TWE', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (7, N'P', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (8, N'TM', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (9, N'N', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (10, N'W', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (11, N'TSS', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (12, N'HC', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (13, N'NC', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (14, N'WS', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (15, N'NL', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (16, N'TWT', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (17, N'TRR', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (18, N'CP', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (19, N'TCM', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (20, N'TSG', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (21, N'TBC', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (22, N'AV', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (23, N'TDR', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (24, N'TTS', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (25, N'TB', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (26, N'TBA', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (27, N'TBA2', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (28, N'RE', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (29, N'BRV', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (30, N'BCV', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (31, N'NW', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (32, N'BIC', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (33, N'TSRL', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (34, N'BRCBL', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (35, N'WW', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (36, N'ML', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (37, N'FR', 1, N'')
    GO
    INSERT [dbo].[Subdivision_Dim] ([SubdivisionID], [SubdivisionName], [Active], [Comment]) VALUES (38, N'AK', 1, N'')
    GO
    SET IDENTITY_INSERT [dbo].[Subdivision_Dim] OFF
    GO
    



    Friday, July 29, 2016 1:55 PM

Answers

  • User-1826179050 posted

    I tried moving the DataPager code outside of the LayoutTemplate (to the same level as the ListView, SQLDataSource, and DropDownList) of the ListView and set its PagedControlID to the ListView's ID and this made the DataPager1 visible to the code behind and scripts. It this a bug, or is it some kind of legitimate scope issue?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 29, 2016 4:45 PM