Wrox Programmer Forums
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #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?

AXE
  #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.

Code:
<%@ 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

Code:
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.


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





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