Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional Web Parts and Custom Controls ASP.NET ISBN: 0-7645-7860-X
This is the forum to discuss the Wrox book Professional Web Parts and Custom Controls with ASP.NET 2.0 by Peter Vogel; ISBN: 9780764578601
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Web Parts and Custom Controls ASP.NET ISBN: 0-7645-7860-X section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 4th, 2006, 04:44 AM
Axe Axe is offline
Registered User
Join Date: Nov 2006
Location: Sydney, , Australia.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Accessing controls in host page from User Control

Hi all

After reading chapter 4 I thought this is really simple but I must have missed something. What I have is a basic entry form in AddEngineer.ascx. I created properties for each control in AddEngineer.ascx.cs.
(I reduced to three properties for example)

using System;
using System.Web.UI;

public partial class AddEngineer : UserControl
    private string _name;
    private string _email;
    private string _web;

    public string Name
        get { return _name; }
        set { _name = value; }

    public string Email
        get { return _email; }
        set { _email = value; }

    public string Web
        get { return _web; }
        set { _web = value; }
     protected void Page_PreRender(object sender, EventArgs e)
        txtName.Text = Name;
        txtEmail.Text = Email;
        txtWeb.Text = Web;

Then in .aspx.cs file:

using System;
using System.Web.UI;
using ASP;

public partial class Admin_Industry : Page
    public admin_usercontrols_addengineer_ascx uc;

    protected void Page_Load(object sender, EventArgs e)
        uc.Name = "Some Name";
        uc.Email = "Some Email";
        uc.Web = "Some Email";

No IntelliSense error but runtime error:

Object reference not set to an instance of an object.
I'm guessing in aspx Page_Load the User Control hasn't loaded yet. I read the bit on "Simulating a Constuctor" and played around with it but havn't got it to work. If you read this and know can you alter the code I provided so I can get the idea straight away.

I can even set properties in Visual Studio's Property Panel but I need programmatic access and besides this should be simple. What did I miss?

  #2 (permalink)  
Old March 16th, 2011, 06:22 AM
Registered User
Join Date: Mar 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Cool Accessing controls in host page from User Control

I know it is a very old post but I have seen many guys asking how to do this and the answer is simple.

Register that usercontrol in the host page and create a control too. Later from code behind use the control id to call a public method in user control.

<%@ Page Title="" Language="C#" AutoEventWireup="true" CodeFile="incidents.aspx.cs" Inherits="WebPages_incidents" %>
<%@ Register Src="~/UserControls/NewCargo.ascx" TagPrefix="uc1" TagName="NewCargo" %>

some html code

<uc1:NewCargo ID="newCargo1" runat="server" />
now in code behind

newCargo1.showPopupNewCargo(lblIncidentID.Text, "");
'newCargo1' is the control id of that usercontrol and 'showPopupNewCargo' is a public method in usercontrol.

Hope i was any helpful.

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing the Controls of a User Control? Aaron Edwards ASP.NET 2.0 Basics 6 June 16th, 2006 07:22 PM
Accessing Properties in User Controls andyj00 ASP.NET 1.0 and 1.1 Professional 16 August 1st, 2005 07:55 AM
Accessing Properties in User Controls andyj00 Classic ASP Professional 1 May 21st, 2005 02:52 PM
Accessing Web User Controls from code aquaboltar ASP.NET 2.0 Basics 0 October 26th, 2004 04:04 PM

All times are GMT -4. The time now is 11:21 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.