using ascx file within an aspx file

Posted on 2008-11-12
Last Modified: 2012-05-05
I have about 5 radio buttons and 3 drop downs that are used and are the same in 3 different pages, instead of duplicating the code in all 3 pages, I thought to use a web user control....the problem is I dont know how to source the drop down list within the aspx file.
I have code that populates the drop downs, is it to be in the ascx code behind or the aspx code behind. I need to understand how I can get the two to talk to each other.
VS.NET 2003 - C#

the master page is called default.aspx
and the user control is called settings.ascx

is this possible to do, what am I missing?
all input appreciated.
Question by:solraccheffy
    LVL 21

    Expert Comment

    you can source the drop down in the ascx itself in the page load...when it's loaded it should also load data...otherwise, you need to create some methods in the ascx like loadData(string dataToLoad) so you can pass parameters from the aspx page...

    i.e.  dataToLoad would be a query/ an array/ etc...

    you would use the instance that you have on your page to access these message...mycontrol.loadData(textbox.text)  say you're getting your parameter from a user......

    Author Comment

    sorry that was confusing for me...
    are you saying its not easy to do this?
    would it be more useful to copy and duplicate the code within the aspx pages?
    LVL 21

    Accepted Solution

    well i'm saying you just need a method that allows loading of the class...

    you have a mycontrol.ascx...  <-- that's considered and give it an id, etc...

    <controls: mycontrol id="id" runat="server">

    in code-behind:


    in the myControl .ascx class

    you may want to have a method called loadControl...different from pageload that you can load the control with that takes parameters...

    otherwise, it would be simpler to just add to page but that would defeat OOP reusability principals...

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    730 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now