Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3607124
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:20:11+00:00 2026-05-18T21:20:11+00:00

My question is about accessing a server control (listbox) that is located in default.aspx.

  • 0

My question is about accessing a server control (listbox) that is located in default.aspx.

I wish to access this control in Functions.cs (this class is located in the App_Code folder).

My page structure is:

  • 1 masterpage with 1 content holder
  • Default.aspx (all the controls are within the content place holder)
  • Functions.cs (located in App_Code)

Now when I try to fill up the listbox elements I get the error “object reference not set to an instance of an object.”

What I have tried to gain access to this control: (this code is located in Functions.cs in App_Code).
This is basically showing some items in the listbox that are located in an xml file

private static string file = HttpContext.Current.Server.MapPath("~/App_Data/Questions.xml");

public static void ListItems()
{
    XmlDocument XMLDoc = new XmlDocument();
    XMLDoc.Load(file); 
    XPathNavigator nav = XMLDoc.CreateNavigator();

    XPathExpression expr;
    expr = nav.Compile("/root/file/naam");
    XPathNodeIterator iterator = nav.Select(expr);

    //ATTEMPT to get access to ServerControl(listbox)
    Page page = (Page)HttpContext.Current.Handler;
    ListBox test = (ListBox)page.FindControl("lbTest"); //control is called lbTest in Default.aspx

    test.Items.Clear();

    while (iterator.MoveNext())
    {
        test.Items.Add(iterator.Current.Value);
    }
}

Code from the default.apx file

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterFile.master" AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="default" Debug="true" %>

<%@ MasterType TypeName="Master" %>

<asp:Content ID="Content1" ContentPlaceHolderID="cphContent" Runat="Server" >

    <asp:MultiView ID="mvTest" runat="server" >

        <asp:View ID="vCollection" runat="server">
            <asp:ListBox ID="lbTest" runat="server" CssClass="listbox" ></asp:ListBox>
        </asp:View>


    </asp:MultiView>

</asp:Content>

The masterfile itself just has 1 placeholder.

Then I call upon the function ListItems in the Default.aspx.cs file

protected void Page_Load(object sender, EventArgs e)
{    
    Functions.ListItems();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:20:12+00:00Added an answer on May 18, 2026 at 9:20 pm

    You should pass the control you wish to populate with items into your function.

    Function.cs:

    public static void ListItems(ListBox test)
    {
         //same as what you had before, minus the
         //ATTEMPT to get access to ServerControl(listbox)
         //lines
    }
    

    Codebehind:

    protected void Page_Load(object sender, EventArgs e)
    {
        Functions.ListItems(lbTest); //pass your control in here...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is a General Question about MVC .. I wrote a PHP Class that
This question is about accessing YouTube content via a C# WinForms application. The requirements
Im new to rails and have a question about accessing relations of relations in
I'm new to xcode and I've one basic question about relative accessing views in
Question about subclassing in matlab, under the new class system. I've got class A
A question about inheritance in java... class Base { private int val = 10;
I'm accessing a Microsoft Access 2002 database (MDB) using ASP.NET through the OdbcConnection class,
I have a question about login and accessing database for mobile web applications. Namely,
This is semi-related to my previous question . As that previous question states, I
This is a brainstorming question about what's possible in Java (or not). I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.